導航:首頁 > 源碼編譯 > vector源碼

vector源碼

發布時間:2022-02-14 18:54:28

Ⅰ #include <vector>在C++中代表什麼意思啊vector是類模板名么然後是這個類模板的內內部內容是什麼啊

包含 vector 頭文件的意思,這個頭文件當中,描述了一個叫做vector的容器模板。

這個容器包含的內容的類型,要在使用的使用給出,例如
vector<int> idlist ;
這就是模板類的實例化。

具體的實現,你可以打開 vector文件,仔細分析源碼

Ⅱ 去哪裡下載 c++ 標准庫stl vector 的源碼

不用下的,比如你用到了vector,那就必須導入vector是不是?所以在文件開頭就有這么一句:#include <vector>,那麼在vector上右鍵,會有打開文件或者轉到定義之類的選項,選擇就會打開了。當然我說的實在集成開發環境中。

Ⅲ 如何查看c++ vector容器源代碼

1).#include <iostream>
#include <vector>using namespace std;int main()
{
int a[7]={1,2,3,4,5,6,7};
vector<int> va(a,a+7); for(int i=0;i<va.size();i++)
cout<<va[i]<<" ";
} 這個是簡單的遍歷向量,輸出向量全部元素。 2).這是簡單的從向量 test.txt 文本文件中提取數據到向量 vector<string> va 中,然後在輸出。test.txt 的文本內容如下: 運行結果如下://程序代碼如下:#include <iostream>
#include <vector>
#include <fstream>
#include <string>using namespace std;int main()
{
vector<string> va;
ifstream in("test.txt");
for(string s;in>>s;)
va.push_back(s);
for(int i=0;i<va.size();i++)
cout<<va[i]<<" ";
}

Ⅳ VectorAssembler明明載入了,源碼都可以關聯上 idea還說找不到

點擊設置面板(Ctrl+alt+S),選擇「Paths」,在classpath標簽項中將你在globallibraries中創建的項選中就可以啦。在GlobalLibraries創建的項如果不被選擇,是不會添加到項目中的。

Ⅳ C++ vector(入門問題)--200懸賞。(幫我注釋一下)

要熟悉面向對象編程,,,vector<int>中的每一個成員是一個Int ,vector< vector<int> > 中的每一個成員是個vector<int> ,,,所以輸入部分的v[i]代表第i個vector<int> 然後.push_back就是放入第i個vector<int>中

Ⅵ 關於STL vector的實現 答對加100

今天看了下源碼,確實是通過判斷是否是int類型進行不同操作的,具體如下:

首先是這個函數:
template <class _InputIterator>
vector(_InputIterator __first, _InputIterator __last,
const allocator_type& __a = allocator_type()) : _Base(__a) {
typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
_M_initialize_aux(__first, __last, _Integral());
}
定義了一個類型:
typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
這個類型會根據_InputIterator的不同而進行不一樣的類模板顯示具體化,比如說,如果是int類型的話,
__STL_TEMPLATE_NULL struct _Is_integer<int> {
typedef __true_type _Integral;
};

會將__true_type定義為_Integral,其他非數值情況,會用默認的模板實例化:
template <class _Tp> struct _Is_integer {
typedef __false_type _Integral;
};
會將__false_type定義為_Integral

然後調用:
_M_initialize_aux(__first, __last, _Integral());

_M_initialize_aux這個函數有重載,會根據_Integral()是__false_type還是__true_type調用不同的重載函數,
兩個重載如下:
template <class _Integer>
void _M_initialize_aux(_Integer __n, _Integer __value, __true_type) {
_M_start = _M_allocate(__n);
_M_end_of_storage = _M_start + __n;
_M_finish = uninitialized_fill_n(_M_start, __n, __value);
}

template <class _InputIterator>
void _M_initialize_aux(_InputIterator __first, _InputIterator __last,
__false_type) {
_M_range_initialize(__first, __last, __ITERATOR_CATEGORY(__first));
}

之後的就簡單了,就是根據是否是int做出不同的操作就是了

======================================================================
我看的是vc的vector文件,有點不太一樣,呵呵

是我想當然了,確實是用模板類的,不過寫的挺麻煩的、

你可以到網上搜一下stl的源碼,然後用Source Insight這個軟體跟蹤一下

還是看源碼最容易理解,我也得看一下了,不懂的還是很多啊
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
這樣說也有點不明白,給你寫了個簡單點的,代碼如下,我只是寫個簡單的例子,迭代器的實現很麻煩的:
#include <iostream>
using namespace std;
class _It
{
public:
_It(){}
_It(int *a):p(a){}
int* operator ++(int)
{
int *q=p;
p++;
return q;
}
bool operator <=(_It &oth)
{
return (this->p)<=(oth.p);
}
int operator -(_It &oth)
{
return (this->p)-(oth.p);
}
private:
int *p;
};
template<class T>
class myvector
{
public:
myvector(int n,T a)
{
data=new T[n];
for(int i=0;i<n;i++)
data[i]=a;
len=n;
}
myvector(_It s,_It e)
{
len=e-s;
data=new T[len];
T *p=data;
while(s<=e)
*p++=*s++;
}

void print()
{
for(int i=0;i<len;i++)
cout<<data[i]<<" ";
cout<<endl;
}
private:
T *data;
int len;
};
void main()
{
myvector<int> v1( 5, 6 );
v1.print();
int a[] = {1,2,3};
myvector<int> v2(a, a+3);
v2.print();

}
==============================================================
int a[] = {1,2,3}; vector<int> v(a, a+3);
這種實現不是因為模板,而是因為int *這種指針也是迭代器的一種,就是說
int *可以轉化為迭代器,而不是因為模板的類型替換

比如說
class A
{
A(int){}
}
這樣的類,是可以這樣用的:
如果一個函數的定義是:
void func(A x){}
則調用時可以用:
func(5);
因為A中有轉換的構造函數

這里也是一樣的,可以用
int a[] = {1,2,3}; vector<int> v(a, a+3);
是因為int *可以轉化為迭代器,

不知道這樣說明白了沒有

其實我對stl的機制也有很多不明白的
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
vector裡面template中的參數和迭代器不是一個吧,如下:
template<class _Ty, class _A = allocator<_Ty> >

typedef const_iterator _It;
vector(_It _F, _It _L, const _A& _Al = _A())

差不多這個意思:
template<class T>
vector(_Iter _First, _Iter _Last)

不會報錯

Ⅶ c++ stl里的向量vector非常好用,那麼它是怎麼實現的呢

這個要去翻源碼了,STL里的代碼說實話,真的看不太懂。

如果不是太糾結於具體細節,可以簡單講講基本的實現思路,大致如下:

  1. vector從功能上來講,屬於順序存儲容器,所以底層實現一般基於數組。

  2. vector使用模板元編程技術實現,具體一點就是編譯器根據使用時指定的實際類型在編譯時執行模板特化,編譯出對應的代碼。也就是說vector<int> v1; vector<double>v2;它們各對應一個特化版本的代碼。這提高了代碼的抽象級別,但是對帶來了代碼膨脹的問題。

  3. vector的重要特性之一就是實現了數組的動態遞增。簡單來說就是容器內部記錄當前的足最大容量和使用量。當添加元素的時候,如果容器類發現當前的容量已耗盡,容器類會自動地重新分配一個更大容量的數組,把當前的所有元素過去,然後釋放掉舊的數組,從而實現動態自增,這一切對使用者來說完全透明。

  4. vector提供迭代器來提供統一的遍歷訪問介面,方便與STL中的其它組件進行交互。



這其中會有很多的細節,比如:

1. 是否允許vector在必要時縮小自身容量?

2. vector容量耗盡後的遞增量是多少?

3. 是否應該提供線程安全容器?

有些東西可能真的需要去翻源碼去看才能搞明白。或者可以參考侯捷的《STL源碼剖析》。其實vector本身的實現並不會太復雜,它的實現思路也很簡單,但是設計層面的一些取捨就需要經過仔細考量了。一般來說,STL是一個足夠堅實的後盾,我們會頻繁地使用它,以構建健壯高效的軟體。能夠理解STL里的一些設計思想和實現方式,對提高我們的編程思維和編程能力會所幫助。

Ⅷ 求STL中vector中insert函數的源代碼 最好加上注釋

還是直接看vector standard header比較好,直接貼出來不好閱讀。
源代碼VC和VS的目錄里有,比如VC8路徑:C:\Program Files\Microsoft Visual Studio 8\VC\include里的vector。
//insert
_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,
const _Myt& _Right, size_type _Roff, size_type _Count)
{ // insert _Right [_Roff, _Roff + _Count) at _Off
if (_Mysize < _Off || _Right.size() < _Roff)
_String_base::_Xran(); // _Off or _Roff off end
size_type _Num = _Right.size() - _Roff;
if (_Num < _Count)
_Count = _Num; // trim _Count to size
if (npos - _Mysize <= _Count)
_String_base::_Xlen(); // result too long

if (0 < _Count && _Grow(_Num = _Mysize + _Count))
{ // make room and insert new stuff
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off, _Mysize - _Off); // empty out hole
if (this == &_Right)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + (_Off < _Roff ? _Roff + _Count : _Roff),
_Count); // substring
else
_Traits_helper::_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Right._Myptr() + _Roff, _Count); // fill hole
_Eos(_Num);
}
return (*this);
}

_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,
const _Elem *_Ptr, size_type _Count)
{ // insert [_Ptr, _Ptr + _Count) at _Off
if (_Inside(_Ptr))
return (insert(_Off, *this,
_Ptr - _Myptr(), _Count)); // substring
if (_Mysize < _Off)
_String_base::_Xran(); // _Off off end
if (npos - _Mysize <= _Count)
_String_base::_Xlen(); // result too long
size_type _Num;
if (0 < _Count && _Grow(_Num = _Mysize + _Count))
{ // make room and insert new stuff
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off, _Mysize - _Off); // empty out hole
_Traits_helper::_s<_Traits>(_Myptr() + _Off, _Myres - _Off, _Ptr, _Count); // fill hole
_Eos(_Num);
}
return (*this);
}

_Myt& __CLR_OR_THIS_CALL insert(size_type _Off, const _Elem *_Ptr)
{ // insert [_Ptr, <null>) at _Off
return (insert(_Off, _Ptr, _Traits::length(_Ptr)));
}

_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,
size_type _Count, _Elem _Ch)
{ // insert _Count * _Ch at _Off
if (_Mysize < _Off)
_String_base::_Xran(); // _Off off end
if (npos - _Mysize <= _Count)
_String_base::_Xlen(); // result too long
size_type _Num;
if (0 < _Count && _Grow(_Num = _Mysize + _Count))
{ // make room and insert new stuff
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off, _Mysize - _Off); // empty out hole
_Chassign(_Off, _Count, _Ch); // fill hole
_Eos(_Num);
}
return (*this);
}

iterator __CLR_OR_THIS_CALL insert(iterator _Where)
{ // insert <null> at _Where
return (insert(_Where, _Elem()));
}

iterator __CLR_OR_THIS_CALL insert(iterator _Where, _Elem _Ch)
{ // insert _Ch at _Where
size_type _Off = _Pdif(_Where, begin());
insert(_Off, 1, _Ch);
return (begin() + _Off);
}

void __CLR_OR_THIS_CALL insert(iterator _Where, size_type _Count, _Elem _Ch)
{ // insert _Count * _Elem at _Where
size_type _Off = _Pdif(_Where, begin());
insert(_Off, _Count, _Ch);
}

template<class _It>
void __CLR_OR_THIS_CALL insert(iterator _Where, _It _First, _It _Last)
{ // insert [_First, _Last) at _Where
_Insert(_Where, _First, _Last, _Iter_cat(_First));
}

template<class _It>
void __CLR_OR_THIS_CALL _Insert(iterator _Where, _It _Count, _It _Ch,
_Int_iterator_tag)
{ // insert _Count * _Ch at _Where
insert(_Where, (size_type)_Count, (_Elem)_Ch);
}

template<class _It>
void __CLR_OR_THIS_CALL _Insert(iterator _Where, _It _First, _It _Last,
input_iterator_tag)
{ // insert [_First, _Last) at _Where, input iterators
replace(_Where, _Where, _First, _Last);
}

void __CLR_OR_THIS_CALL insert(iterator _Where, const_pointer _First, const_pointer _Last)
{ // insert [_First, _Last) at _Where, const pointers
replace(_Where, _Where, _First, _Last);
}

void __CLR_OR_THIS_CALL insert(iterator _Where, const_iterator _First, const_iterator _Last)
{ // insert [_First, _Last) at _Where, const_iterators
replace(_Where, _Where, _First, _Last);
}

//replace
_Myt& __CLR_OR_THIS_CALL replace(size_type _Off, size_type _N0, const _Myt& _Right)
{ // replace [_Off, _Off + _N0) with _Right
return (replace(_Off, _N0, _Right, 0, npos));
}

_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,
size_type _N0, const _Myt& _Right, size_type _Roff, size_type _Count)
{ // replace [_Off, _Off + _N0) with _Right [_Roff, _Roff + _Count)
if (_Mysize < _Off || _Right.size() < _Roff)
_String_base::_Xran(); // _Off or _Roff off end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off; // trim _N0 to size
size_type _Num = _Right.size() - _Roff;
if (_Num < _Count)
_Count = _Num; // trim _Count to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen(); // result too long

size_type _Nm = _Mysize - _N0 - _Off; // length of preserved tail
size_type _Newsize = _Mysize + _Count - _N0;
if (_Mysize < _Newsize)
_Grow(_Newsize);

if (this != &_Right)
{ // no overlap, just move down and in new stuff
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // empty hole
_Traits_helper::_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Right._Myptr() + _Roff, _Count); // fill hole
}
else if (_Count <= _N0)
{ // hole doesn't get larger, just in substring
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + _Roff, _Count); // fill hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
}
else if (_Roff <= _Off)
{ // hole gets larger, substring begins before hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + _Roff, _Count); // fill hole
}
else if (_Off + _N0 <= _Roff)
{ // hole gets larger, substring begins after hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + (_Roff + _Count - _N0), _Count); // fill hole
}
else
{ // hole gets larger, substring begins in hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + _Roff, _N0); // fill old hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _N0, _Myres - _Off - _N0, _Myptr() + _Roff + _Count,
_Count - _N0); // fill rest of new hole
}

_Eos(_Newsize);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,
size_type _N0, const _Elem *_Ptr, size_type _Count)
{ // replace [_Off, _Off + _N0) with [_Ptr, _Ptr + _Count)
if (_Inside(_Ptr))
return (replace(_Off, _N0, *this,
_Ptr - _Myptr(), _Count)); // substring, replace carefully
if (_Mysize < _Off)
_String_base::_Xran(); // _Off off end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off; // trim _N0 to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen(); // result too long
size_type _Nm = _Mysize - _N0 - _Off;

if (_Count < _N0)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // smaller hole, move tail up
size_type _Num;
if ((0 < _Count || 0 < _N0) && _Grow(_Num = _Mysize + _Count - _N0))
{ // make room and rearrange
if (_N0 < _Count)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Traits_helper::_s<_Traits>(_Myptr() + _Off, _Myres - _Off, _Ptr, _Count); // fill hole
_Eos(_Num);
}
return (*this);
}

_Myt& __CLR_OR_THIS_CALL replace(size_type _Off, size_type _N0, const _Elem *_Ptr)
{ // replace [_Off, _Off + _N0) with [_Ptr, <null>)
return (replace(_Off, _N0, _Ptr, _Traits::length(_Ptr)));
}

_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,
size_type _N0, size_type _Count, _Elem _Ch)
{ // replace [_Off, _Off + _N0) with _Count * _Ch
if (_Mysize < _Off)
_String_base::_Xran(); // _Off off end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off; // trim _N0 to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen(); // result too long
size_type _Nm = _Mysize - _N0 - _Off;

if (_Count < _N0)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // smaller hole, move tail up
size_type _Num;
if ((0 < _Count || 0 < _N0) && _Grow(_Num = _Mysize + _Count - _N0))
{ // make room and rearrange
if (_N0 < _Count)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Chassign(_Off, _Count, _Ch); // fill hole
_Eos(_Num);
}
return (*this);
}

_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Myt& _Right)
{ // replace [_First, _Last) with _Right
return (replace(
_Pdif(_First, begin()), _Pdif(_Last, _First), _Right));
}

_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Elem *_Ptr,
size_type _Count)
{ // replace [_First, _Last) with [_Ptr, _Ptr + _Count)
return (replace(
_Pdif(_First, begin()), _Pdif(_Last, _First), _Ptr, _Count));
}

_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Elem *_Ptr)
{ // replace [_First, _Last) with [_Ptr, <null>)
return (replace(
_Pdif(_First, begin()), _Pdif(_Last, _First), _Ptr));
}

_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,
size_type _Count, _Elem _Ch)
{ // replace [_First, _Last) with _Count * _Ch
return (replace(
_Pdif(_First, begin()), _Pdif(_Last, _First), _Count, _Ch));
}

template<class _It>
_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,
_It _First2, _It _Last2)
{ // replace [_First, _Last) with [_First2, _Last2)
return (_Replace(_First, _Last,
_First2, _Last2, _Iter_cat(_First2)));
}

template<class _It>
_Myt& __CLR_OR_THIS_CALL _Replace(iterator _First, iterator _Last,
_It _Count, _It _Ch, _Int_iterator_tag)
{ // replace [_First, _Last) with _Count * _Ch
return (replace(_First, _Last, (size_type)_Count, (_Elem)_Ch));
}

template<class _It>
_Myt& __CLR_OR_THIS_CALL _Replace(iterator _First, iterator _Last,
_It _First2, _It _Last2, input_iterator_tag)
{ // replace [_First, _Last) with [_First2, _Last2), input iterators
_Myt _Right(_First2, _Last2);
replace(_First, _Last, _Right);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,
const_pointer _First2, const_pointer _Last2)
{ // replace [_First, _Last) with [_First2, _Last2), const pointers
if (_First2 == _Last2)
erase(_Pdif(_First, begin()), _Pdif(_Last, _First));
else
replace(_Pdif(_First, begin()), _Pdif(_Last, _First),
&*_First2, _Last2 - _First2);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,
const_iterator _First2, const_iterator _Last2)
{ // replace [_First, _Last) with [_First2, _Last2), const_iterators
if (_First2 == _Last2)
erase(_Pdif(_First, begin()), _Pdif(_Last, _First));
else
replace(_Pdif(_First, begin()), _Pdif(_Last, _First),
&*_First2, _Last2 - _First2);
return (*this);
}

Ⅸ Vector源碼

直接看vector文件內容就有
就是先判斷size是否相等 再判斷每個元素是否相等

template<class _Ty,
class _Alloc> inline
bool operator==(const vector<_Ty, _Alloc>& _Left,
const vector<_Ty, _Alloc>& _Right)
{ // test for vector equality
return (_Left.size() == _Right.size()
&& equal(_Left.begin(), _Left.end(), _Right.begin()));
}

template<class _Ty,
class _Alloc> inline
bool operator!=(const vector<_Ty, _Alloc>& _Left,
const vector<_Ty, _Alloc>& _Right)
{ // test for vector inequality
return (!(_Left == _Right));
}

Ⅹ 求c++源代碼

// ============================================================================
// 類名:main
// 說明:英文單詞拼寫檢查,並推薦正確單詞示例源碼
// 備註:
// 編寫:徐景周([email protected])
// 組織:未來工作室(Future Studio)
// 日期:2005.12.1
// =============================================================================
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cctype>

#include "dictionary.h"

using namespace std;

void lower( string& s );
string strip_punct( const string& s );
void check_spelling( ifstream& in, Dictionary& dict );

int main( int argc, char* argv[] )
{

// 如果命令行參數不對,提示錯誤信息
if (argc != 3)
{
cerr << "Usage: " << argv[0] << " wordlist_filename input_file\n";
return EXIT_FAILURE;
}

ifstream inf(argv[2]);
if (! inf)
{
cerr << "Could not open " << argv[2] << "\n";
return EXIT_FAILURE;
}

// 開始導入字典到哈希表中
cout << "Loading dictionary, this may take awhile...\n";

Dictionary d(argv[1]);

check_spelling(inf, d);

inf.close();

// 調試時起暫停作用
system("pause");

return EXIT_SUCCESS;
}

// 單詞內全部字母兩兩交換後,在字典中查找是否正確,正確則輸出,否則
// 繼續上述操作直到全部交換過為止
void AllLetter_Swap( int nPos, const string& word, Dictionary& dict )
{
// 直到單詞最後一個字母,結束遞歸
if( nPos == word.length() )
return;

string strWord;
char chLetter = word[nPos];
for( int j = nPos+1; j < word.length(); ++j )
{
// 恢復原始單詞值
strWord = word;

// 互換指定nPos位置與其後字母
strWord[nPos] = strWord[j];
strWord[j] = chLetter;

// 字典中查找,找到輸出
if( dict.search( strWord ) )
cout << "\t\t" << strWord << endl;
}

// 遞歸調用
AllLetter_Swap( nPos+1, word, dict);
}

// 單詞內相鄰兩字母交換後,在字典中查找是否正確,正確則輸出,否則
// 繼續上述操作直到最後兩字母交換過為止
void AdjacentLetter_Swap( const string& word, Dictionary& dict )
{
string strWord;
for( int nPos = 0; nPos < word.length()-1; ++nPos )
{
// 恢復原始單詞值
strWord = word;

// 兩相鄰字母互換(當前字母與相鄰後面一個字母互換)
char chLetter = word[nPos];
strWord[nPos] = strWord[nPos+1];
strWord[nPos+1] = chLetter;

// 字典中查找,找到輸出
if( dict.search( strWord ) )
cout << "\t\t" << strWord << endl;
}
}

// 逐次刪除單詞中每個字母後,在字典中查找是否正確,正確則輸出
void RemoveLetter( const string& word, Dictionary& dict )
{
vector<string> vecWord; // 存放刪除單詞字母後,正確單詞的數組,用於避免有重復的正確單詞輸出
string strWord;
for( int nPos = 0; nPos < word.length(); ++nPos )
{
// 恢復原始單詞值
strWord = word;

// 刪除一個字母
strWord.erase( nPos, 1 );

// 字典中查找,找到輸出
if( dict.search( strWord ) )
{
// 在前一次正確單詞的數組中查找,如果存在的話,不再輸出和壓入到數組
vector<string>::iterator Iter = vecWord.begin();
for( ; Iter != vecWord.end(); ++Iter )
{
if( (*Iter) == strWord )
break;
}

// 否則不存在,則壓入該正確單詞到數組並輸出
if( Iter == vecWord.end() )
{
vecWord.push_back( strWord );
cout << "\t\t" << strWord << endl;
}
}
}
}

// 逐次替換單詞中每個字母為其它一個字母,在字典中查找是否正確,正確則輸出
void ReplaceLetter( const string& word, Dictionary& dict )
{
string strWord;
string strAlpha = "abcdefghigklmnopqrstuvwxyz"; // 26個小寫字母
for( int nPos = 0; nPos < word.length(); ++nPos )
{
// 單詞中逐次將每位字母用26個字母代替,判斷是否正確單詞
for( int nAlpha = 0; nAlpha < strAlpha.length(); ++nAlpha )
{
// 恢復原始單詞值
strWord = word;

// 將單詞strWord中nPos位置開始的1個字母,用字母串
// strAlpha中的nAlpha位置開始的1個字母代替
strWord.replace( nPos, 1, strAlpha, nAlpha, 1 );

// 字典中查找,找到輸出
if( dict.search( strWord ) )
cout << "\t\t" << strWord << endl;
}

}
}

// 逐次在單詞中任意位置,插入任意一個字母後,在字典中查找是否正確,正確則輸出
void InsertLetter( const string& word, Dictionary& dict )
{
vector<string> vecWord; // 存放插入單詞字母後,正確單詞的數組,用於避免有重復的正確單詞輸出
string strWord;
string strAlpha = "abcdefghigklmnopqrstuvwxyz"; // 26個小寫字母
for( int nPos = 0; nPos < word.length(); ++nPos )
{
// 單詞中逐次在每個位置中插入26個字母中一個後,判斷是否正確單詞
for( int nAlpha = 0; nAlpha < strAlpha.length(); ++nAlpha )
{
// 恢復原始單詞值
strWord = word;

// 從單詞strWord中nPos位置開始,插入字母串
// strAlpha中的nAlpha位置開始的1個字母
strWord.insert( nPos, strAlpha, nAlpha, 1 );

// 字典中查找,找到輸出
if( dict.search( strWord ) )
{
// 在前一次正確單詞的數組中查找,如果存在的話,不再輸出和壓入到數組
vector<string>::iterator Iter = vecWord.begin();
for( ; Iter != vecWord.end(); ++Iter )
{
if( (*Iter) == strWord )
break;
}

// 否則不存在,則壓入該正確單詞到數組並輸出
if( Iter == vecWord.end() )
{
vecWord.push_back( strWord );
cout << "\t\t" << strWord << endl;
}
}
}

}
}

// 單詞與哈希表中字典對照,進行拼寫檢查
void check_spelling( ifstream& in, Dictionary& dict )
{

int line_number = 0;

while (in)
{

line_number++;

// 從測試文件中讀入一行
string line;
getline(in, line);

// 將讀入行放入stringstream中
stringstream ss (stringstream::in | stringstream::out);
ss << line;

// 利用stringstream將一行字元串中每一單詞自動提取出來到word中
string word;
while (ss >> word)
{
// 將提取出單詞轉成小寫,並去掉可能存在的尾部標點
lower( word );
word = strip_punct( word );

// 在哈希字典里查找這個單詞是否存在,存在表示該單詞正確,
// 直接結束本次循環,讀取下一個單詞重復以上操作
if( dict.search( word ) )
continue;

// 否則在字典里沒找到,表示該單詞是錯誤的,輸出並推薦輸出可能正確的單詞
cout << "line " << line_number << ": '" << word << "'" << endl;
cout << "\t" << "suggestions:" << endl;

// 1. 單詞中全部字母兩兩互換後,判斷是否正確單詞
// AllLetter_Swap( 0, word, dict );
// 1. 單詞中相鄰兩字母互換後,判斷是否正確單詞
AdjacentLetter_Swap( word, dict );

// 2. 刪除單詞中單個字母後,判斷是否正確單詞
RemoveLetter( word, dict );

// 3. 逐次替換單詞中每個字母為其它字母,判斷是否正確單詞
ReplaceLetter( word, dict );

// 4. 逐次在單詞中任意位置,插入任意一個字母後,判斷是否正確單詞
InsertLetter( word, dict );
}

}

}

// 將單詞字元串轉換為小寫字元串
void lower( string& s )
{
for (int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
}
}

// 刪除單詞字元串尾部可能存在的標點符號
string strip_punct( const string& s )
{

if ( ispunct(s[s.length() - 1]) )
{
return s.substr (0, s.length() - 1);
}
else
{
return s;
}
}

閱讀全文

與vector源碼相關的資料

熱點內容
macppt壓縮軟體 瀏覽:131
公眾號推廣系統源碼 瀏覽:61
程序員作息安排 瀏覽:621
如何在本地登錄伺服器 瀏覽:334
喵吧app怎麼使用 瀏覽:751
家庭伺服器如何連wifi 瀏覽:205
新聞推薦系統源碼 瀏覽:224
php中文星號 瀏覽:502
伺服器4盤是什麼意思 瀏覽:594
如何重啟或關閉伺服器 瀏覽:348
pdf文檔加水印 瀏覽:836
機構搶籌指標公式源碼 瀏覽:266
linux腳本awk 瀏覽:558
程序員怎麼跟領導提升 瀏覽:75
pdf怎麼生成目錄 瀏覽:387
如何保護自己的伺服器 瀏覽:69
html5上傳圖片壓縮 瀏覽:473
支付寶賬單文件如何解壓 瀏覽:860
查看內核版本命令 瀏覽:957
w10加密盤驅動鎖死怎麼辦 瀏覽:948