[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)
{
itor2 = itor1;
vec.erase(itor2); //刪除指定位置的元素
itor1--;
}
}
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;
itor1 = vec.begin();
for(itor1 = vec.begin(); itor1 != vec.end(); itor1++)
{
if(6 == *itor1)
{
itor2 = itor1;
vec.erase(itor2); //刪除指定位置的元素
itor1--;
}
}
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++;
}
}