64,648
社区成员
发帖
与我相关
我的任务
分享
/*
let's look up MSDN!
iterator insert(iterator position, const T& x);
Inserts x before position. The return value points to the inserted x.
void insert(iterator position, size_type n, const T& x);
Inserts n copies of x before position.
template <class InputIterator>
void insert(iterator position, InputIterator first, InputIterator last);
Inserts copies of the elements in the range [first, last] before position.
// iter的指向是不确定的。
*/
#include <vector>
#include <iostream>
//#include <algorithm>
using namespace std;
int main()
{
int a[12]={0,1,1,1,1,1,1,0,0,1,0,1};
int temp=0;
int state=0;
vector<int> vec(a,a+12);
vector<int>::iterator iter;
for(iter=vec.begin();iter!=vec.end();iter++)
cout<<*iter<<" ";
cout<<endl;
iter=vec.begin();
while(iter!=vec.end())
{
temp=*iter;
iter++;
if(temp)
state+=1;
else
{
state=0;
cout<<"case 0"<<endl;
continue;
}
switch(state)
{
case 1:
cout<<"case 1"<<endl;
break;
case 2:
cout<<"case 2"<<endl;
break;
case 3:
cout<<"case 3"<<endl;
break;
case 4:
cout<<"case 4"<<endl;
iter=vec.insert(iter ,0); // here
cout<<*iter<<"!!!!"<<endl;
break;
default:
cout<<"default!"<<endl;
break;
}
}
cout<<"///////////////";
for(iter=vec.begin();iter!=vec.end();iter++)
cout<<*iter<<" ";
return 1;
}
template <class T, class Alloc>
void vector<T, Alloc>::insert(iterator position, size_type n, const T& x) {
if (n != 0) {
if (size_type(end_of_storage - finish) >= n) {
T x_copy = x;
const size_type elems_after = finish - position;
iterator old_finish = finish;
if (elems_after > n) {
uninitialized_copy(finish - n, finish, finish);
finish += n;
copy_backward(position, old_finish - n, old_finish);
fill(position, position + n, x_copy);
}
else {
uninitialized_fill_n(finish, n - elems_after, x_copy);
finish += n - elems_after;
uninitialized_copy(position, old_finish, finish);
finish += elems_after;
fill(position, old_finish, x_copy);
}
}
else {
const size_type old_size = size();
const size_type len = old_size + max(old_size, n);
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
__STL_TRY {
new_finish = uninitialized_copy(start, position, new_start);
new_finish = uninitialized_fill_n(new_finish, n, x);
new_finish = uninitialized_copy(position, finish, new_finish);
}
# ifdef __STL_USE_EXCEPTIONS
catch(...) {
destroy(new_start, new_finish);
data_allocator::deallocate(new_start, len);
throw;
}
# endif /* __STL_USE_EXCEPTIONS */
destroy(start, finish);
deallocate();
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}
}
}
/*
参考<<STL 源码剖析>>
由于vector 空间是连续的,在进行插入操作的的时候,存在空间的重新分配、数据移动的操作,插入前的迭代器可能无效。
而list 是链式结构,就不会出现这种情况!
*/
// you are right! compare this with ups! the element which the iterator points is changing in vector.
#include <vector>
#include <iostream>
#include <algorithm>
int main()
{
std::vector<int> l;
l.insert(l.begin(),1);
l.insert(l.begin(),2);
l.insert(l.begin(),3);
std::cout<<"插入前list:";
std::copy(l.begin(),l.end(),std::ostream_iterator<int>(std::cout," "));
std::cout<<std::endl;
std::vector<int>::iterator it=l.begin();
it++;
std::cout<<"插入前it的位置为:"<<*it<<std::endl;
l.insert(it,10);// 插入数据
std::cout<<"插入后it的位置为:"<<*it<<std::endl;
std::cout<<"插入后list:";
std::copy(l.begin(),l.end(),std::ostream_iterator<int>(std::cout," "));
return 1;
}
// give an example to you !
#include <list>
#include <iostream>
#include <algorithm>
int main()
{
std::list<int> l;
l.insert(l.begin(),1);
l.insert(l.begin(),2);
l.insert(l.begin(),3);
std::cout<<"插入前list:";
std::copy(l.begin(),l.end(),std::ostream_iterator<int>(std::cout," "));
std::cout<<std::endl;
std::list<int>::iterator it=l.begin();
it++;
std::cout<<"插入前it的位置为:"<<*it<<std::endl;
l.insert(it,10);// 插入数据
std::cout<<"插入后it的位置为:"<<*it<<std::endl;
std::cout<<"插入后list:";
std::copy(l.begin(),l.end(),std::ostream_iterator<int>(std::cout," "));
return 1;
}
#include <list>
#include <iostream>
int main( )
{
using namespace std;
list <int> c1, c2;
list <int>::iterator Iter;
c1.push_back( 10 );
c1.push_back( 20 );
c1.push_back( 30 );
c2.push_back( 40 );
c2.push_back( 50 );
c2.push_back( 60 );
cout << "c1 =";
for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
Iter = c1.begin( );
Iter++;
c1.insert( Iter, 100 );
cout << "c1 =";
for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
}