有关迭代器和向量的查询 [英] Query regarding iterators and vector
问题描述
我是新的STL编程。
我有关于向量的查询。如果我使用迭代器迭代向量
,但做一些修改
向量大小的操作。迭代器是否会认识到这一点?
我编写了以下程序来测试它。
#include< fstream>
#include< iostream>
#include< string>
#include< vector>
使用命名空间std;
int main()
{
vector< inta;
a.push_back(1);
a.push_back(2);
a.push_back(3);
cout< <" Vector test begin"<< endl;
vector< int> :: iterator iter0;
for(iter0 = a.begin(); iter0 != a.end(); iter0 ++)
{
cout<< \ n值: << (* iter0)<< endl;
if(* iter0 == 2)
{
a.push_back(4);
a.push_back(5);
}
}
cout<<"矢量测试结束" ;;
}
我得到以下输出。有人可以解释一下我的程序中发生了什么
。我很困惑。
--------------------------------- ------输出------------------
矢量测试开始
值: 1
价值:2
价值:3
价值:4
价值:0
价值:41
价值:1
值:2
价值:3
价值:4
价值: 5
价值:4
价值:5
矢量测试结束
----------------------------------------------结束
输出--------------------------------------
I am new STL programming.
I have a query regarding vectors. If I am iterating over a vector
using a iterator, but do some operations that modify the size of the
vector. Will the iterator recognize this?
I wrote the following program to test this out.
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<inta;
a.push_back(1);
a.push_back(2);
a.push_back(3);
cout<<"Vector test begin"<<endl;
vector<int>::iterator iter0;
for(iter0=a.begin(); iter0!=a.end(); iter0++)
{
cout << "\n value: " << (*iter0)<<endl;
if(*iter0 == 2)
{
a.push_back(4);
a.push_back(5);
}
}
cout<<"Vector test end";
}
I am getting the following output. Could somebody please explain what
is happening in my program. I am thoroughly confused.
---------------------------------------Output------------------
Vector test begin
value: 1
value: 2
value: 3
value: 4
value: 0
value: 41
value: 1
value: 2
value: 3
value: 4
value: 5
value: 4
value: 5
Vector test end
----------------------------------------------End of
Output--------------------------------------
推荐答案
pr ***** *****@gmail.com 写道:
pr**********@gmail.com writes:
我是新的STL编程。
我有一个查询向量。如果我使用迭代器迭代向量
,但做一些修改
向量大小的操作。迭代器会认出这个吗?
I am new STL programming.
I have a query regarding vectors. If I am iterating over a vector
using a iterator, but do some operations that modify the size of the
vector. Will the iterator recognize this?
否。在std :: vector中插入或删除会使所有现有的
迭代器无效。
如果希望迭代器在插入后保持有效,则使用std :: list(如果迭代器没有指向std :: list成员,则使用
删除
已删除)。
----- BEGIN PGP SIGNATURE -----
版本:GnuPG v1.4.9(GNU / Linux)>
iEYEABECAAYFAkhuo3oACgkQx9p3GYHlUOLeyACfUNo / pQKJ4Bt8lX36K0KysbUG
5zYAn2o9oKalz38aDj3p69vXn + PLsz / f
= 7gEI
----- END PGP签名-----
No. An insertion or deletion in a std::vector invalidates all existing
iterators.
Use std::list if you want iterators to remain valid after an insertion (or a
deletion, if the iterator does not point to the std::list member that was
deleted).
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEABECAAYFAkhuo3oACgkQx9p3GYHlUOLeyACfUNo/pQKJ4Bt8lX36K0KysbUG
5zYAn2o9oKalz38aDj3p69vXn+PLsz/f
=7gEI
-----END PGP SIGNATURE-----
pr **********@gmail.com 写道:
pr**********@gmail.com wrote:
我是新的STL编程。
我有关于矢量的查询。如果我使用迭代器迭代向量
,但做一些修改
向量大小的操作。迭代器会认出这个吗?
I am new STL programming.
I have a query regarding vectors. If I am iterating over a vector
using a iterator, but do some operations that modify the size of the
vector. Will the iterator recognize this?
No.
当重新分配内存时,向量'的迭代器无效。
此外,在
向量中间插入或删除元素会使指向
插入或删除点后面的元素的所有迭代器无效。因此,如果你使用reserve()来$ / b $ b预分配尽可能多的内存,你可以防止
向量的迭代器失效,并且如果所有
插入和删除都在向量的末尾。
< http://www.sgi.com/tech/stl/Vector.html>
No.
A vector''s iterators are invalidated when its memory is reallocated.
Additionally, inserting or deleting an element in the middle of a
vector invalidates all iterators that point to elements following the
insertion or deletion point. It follows that you can prevent a
vector''s iterators from being invalidated if you use reserve() to
preallocate as much memory as the vector will ever use, and if all
insertions and deletions are at the vector''s end.
<http://www.sgi.com/tech/stl/Vector.html>
我编写了以下程序来测试它。
I wrote the following program to test this out.
尝试添加a.reserve(5);在定义''a''之后的任何时刻,以及
循环之前的任何时刻。
Try adding "a.reserve(5);" at any point after ''a'' is defined, and before
the loop.
#include< fstream>
#include< iostream>
#include< string>
#include< vector>
使用命名空间std;
int main()
{
vector< inta;
a.push_back(1);
a.push_back(2);
a.push_back(3);
cout< ;<" Vector test begin"<< endl;
vector< int> :: iterator iter0;
for(iter0 = a.begin(); iter0!= a.end(); iter0 ++)
{
cout<< \ n值: << (* iter0)<< endl;
if(* iter0 == 2)
{
a.push_back(4);
a.push_back(5);
}
}
cout<<"矢量测试结束" ;;
}
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<inta;
a.push_back(1);
a.push_back(2);
a.push_back(3);
cout<<"Vector test begin"<<endl;
vector<int>::iterator iter0;
for(iter0=a.begin(); iter0!=a.end(); iter0++)
{
cout << "\n value: " << (*iter0)<<endl;
if(*iter0 == 2)
{
a.push_back(4);
a.push_back(5);
}
}
cout<<"Vector test end";
}
谢谢大家的回复。我使用循环变量和
vector.size()而不是迭代器来解决我的问题。
7月4日,6:31 * pm, Daniel T. < danie ... @ earthlink.netwrote:
Thank you all for the replies. I used a loop variable and
vector.size() instead of the iterator to solve my problem.
On Jul 4, 6:31*pm, "Daniel T." <danie...@earthlink.netwrote:
prasadmpa ... @ gmail.com写道:
prasadmpa...@gmail.com wrote:
我是新的STL编程。
我有关于向量的查询。如果我使用迭代器迭代向量
,但做一些修改
向量大小的操作。迭代器会认出这个吗?
I am new STL programming.
I have a query regarding vectors. If I am iterating over a vector
using a iterator, but do some operations that modify the size of the
vector. Will the iterator recognize this?
No.
* *当重新分配内存时,向量'的迭代器无效。
* *此外,插入或删除
* *向量中间的元素会使指向
* *插入后的元素的所有迭代器无效或删除点。接下来你可以防止如果你使用reserve()来使
* *向量'的迭代器无效,那么
* *预分配尽可能多的内存,因为向量将永远使用,如果所有
* *插入和删除都在向量的末尾。
* *< http://www.sgi.com/ tech / stl / Vector.html>
No.
* *A vector''s iterators are invalidated when its memory is reallocated.
* *Additionally, inserting or deleting an element in the middle of a
* *vector invalidates all iterators that point to elements following the
* *insertion or deletion point. It follows that you can prevent a
* *vector''s iterators from being invalidated if you use reserve() to
* *preallocate as much memory as the vector will ever use, and if all
* *insertions and deletions are at the vector''s end.
* *<http://www.sgi.com/tech/stl/Vector.html>
我编写了以下程序来测试它。
I wrote the following program to test this out.
尝试添加a.reserve(5);在定义''a''之后的任何时刻,以及
循环之前的任何时刻。
Try adding "a.reserve(5);" at any point after ''a'' is defined, and before
the loop.
#include< fstream>
#include< iostream>
#include< string>
#include< vector>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace std;
int main()
{
* * vector< inta;
int main()
{
* *vector<inta;
* * a.push_back(1);
* * a.push_back(2);
* * a.push_back(3);
* * cout<<"" Vector test begin"<< endl;
* * vector< ; int> :: iterator iter0;
* * for(iter0 = a.begin(); iter0!= a.end(); iter0 ++)
* * {
* * * * * * cout<< \ n值: << (* iter0)<< endl;
* * * * * * if(* iter0 == 2)
* * * * * * {
* * * * * * * * * * a.push_back(4);
* * * * * * * * * * a.push_back(5);
* * * * * *}
* *}
* * cout<<"矢量测试结束;
}
* *a.push_back(1);
* *a.push_back(2);
* *a.push_back(3);
* *cout<<"Vector test begin"<<endl;
* *vector<int>::iterator iter0;
* *for(iter0=a.begin(); iter0!=a.end(); iter0++)
* *{
* * * * * *cout << "\n value: " << (*iter0)<<endl;
* * * * * *if(*iter0 == 2)
* * * * * *{
* * * * * * * * * *a.push_back(4);
* * * * * * * * * *a.push_back(5);
* * * * * *}
* *}
* *cout<<"Vector test end";
}
这篇关于有关迭代器和向量的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!