zoukankan      html  css  js  c++  java
  • 向量容器2

    题3:

    以下代码有什么问题?如何修改?【中国某著名综合软件公司2005年面试题】

    // 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函数进行修改,代码如下:

    // 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--,再回到原来的位置上。具体代码修改如下:

    // 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++;
    	}
    }
    



  • 相关阅读:
    java 的三种代理模式 (二)——子函数切面
    王者荣耀为什么不使用微服务架构,服务的极简主义,为什么交易网关使用redis做持久
    tcp_syncookies 半连接
    tcp_tw_recycle tcp_tw_reuse与timewait【yetdone】
    动态代理,没有被代理对象
    一次jstack解决update停顿
    动态代理反向
    注解的继承
    51单片机状态机键盘检测
    28335scififo中断接收与发送
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3167821.html
Copyright © 2011-2022 走看看