向量容器2
[cpp] // P96_example3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
void print(std::vector<int>);
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(6);
vec.push_back(6);
vec.push_back(3);
//删除vec数组中的所有6
std::vector<int>::iterator itor1;
std::vector<int>::iterator itor2;
for(itor1 = vec.begin(); itor1 != vec.end(); itor1++)
{
if(6 == *itor1)
{
itor2 = itor1;
vec.erase(itor2); //删除指定位置的元素
}
}
print(vec);
return 0;
}
void print(std::vector<int> v)
{
std::cout<<"vector size is: "<<v.size()<<std::endl;
std::vector<int>::iterator p = v.begin();
while(p != v.end())
{
std::cout<<*p<<std::endl;
p++;
}
}
// P96_example3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
void print(std::vector<int>);
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(6);
vec.push_back(6);
vec.push_back(3);
//删除vec数组中的所有6
std::vector<int>::iterator itor1;
std::vector<int>::iterator itor2;
for(itor1 = vec.begin(); itor1 != vec.end(); itor1++)
{
if(6 == *itor1)
{
itor2 = itor1;
vec.erase(itor2); //删除指定位置的元素
}
}
print(vec);
return 0;
}
void print(std::vector<int> v)
{
std::cout<<"vector size is: "<<v.size()<<std::endl;
std::vector<int>::iterator p = v.begin();
while(p != v.end())
{
std::cout<<*p<<std::endl;
p++;
}
}
解析:
这是迭代器问题,只能删除第一个6,以后迭代器就失效了,不能删除之后的元素。
itor2 = itor1;这句说明两个迭代器是一样的。vec.erase(itor2);等于vec.erase(itor1);,这时指针已经指向下一个元素了。itor1++;又自增了,指向了下一个元素3,略过了第二个6。
答案:
修改方法1:使用vector模版里面的remove函数进行修改,代码如下:
[cpp] // P96_example3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
<SPAN style="COLOR: #ff0000">#include <algorithm></SPAN>
void print(std::vector<int>);
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(6);
vec.push_back(6);
vec.push_back(3);
//删除vec数组中的所有6
std::vector<int>::iterator itor1;
std::vector<int>::iterator itor2;
itor1 = vec.begin();
<SPAN style="COLOR: #ff0000">vec.erase(std::remove(vec.begin(),vec.end(),6),vec.end());</SPAN>
print(vec);
return 0;
}
void print(std::vector<int> v)
{
std::cout<<"vector size is: "<<v.size()<<std::endl;
std::vector<int>::iterator p = v.begin();
while(p != v.end())
{
std::cout<<*p<<std::endl;
p++;
}
}
// P96_example3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
void print(std::vector<int>);
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(6);
vec.push_back(6);
vec.push_back(3);
//删除vec数组中的所有6
std::vector<int>::iterator itor1;
std::vector<int>::iterator itor2;
itor1 = vec.begin();
vec.erase(std::remove(vec.begin(),vec.end(),6),vec.end());
print(vec);
return 0;
}
void print(std::vector<int> v)
{
std::cout<<"vector size is: "<<v.size()<<std::endl;
std::vector<int>::iterator p = v.begin();
while(p != v.end())
{
std::cout<<*p<<std::endl;
p++;
}
}
修改方法2:为了让其不略过第二个6,可以使itor1--,再回到原来的位置上。具体代码修改如下:
[cpp] // P96_example3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
void print(std::vector<int>);
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(6);
vec.push_back(6);
vec.push_back(3);
//删除vec数组中的所有6
std::vector<int>::iterator itor1;
std::vector<int>::iterator itor2;
itor1 = vec.begin();
for(itor1 = vec.begin(); itor1 != vec.end(); itor1++)
{
if(6 == *itor1)
{
&n
补充:软件开发 , C++ ,