导航:首页 > 源码编译 > 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源码相关的资料

热点内容
超准macd副图源码 浏览:6
好脾气的程序员 浏览:661
macppt压缩软件 浏览:131
公众号推广系统源码 浏览:62
程序员作息安排 浏览:621
如何在本地登录服务器 浏览:334
喵吧app怎么使用 浏览:751
家庭服务器如何连wifi 浏览:205
新闻推荐系统源码 浏览:225
php中文星号 浏览:503
服务器4盘是什么意思 浏览:595
如何重启或关闭服务器 浏览:350
pdf文档加水印 浏览:837
机构抢筹指标公式源码 浏览:267
linux脚本awk 浏览:558
程序员怎么跟领导提升 浏览:77
pdf怎么生成目录 浏览:388
如何保护自己的服务器 浏览:70
html5上传图片压缩 浏览:474
支付宝账单文件如何解压 浏览:861