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



  • 相关阅读:
    引领云原生2.0,华为云加速云原生全行业落地!
    【STM32H7】第22章 ThreadX GUIX窗口图标滑动操作实现方法
    【STM32F429】第21章 ThreadX GUIX窗口图标滑动操作实现方法
    【STM32H7】第21章 ThreadX GUIX外置主题,字库和图库到外部SPI Flash
    【STM32F429】第20章 ThreadX GUIX外置主题,字库和图库到外部SPI Flash
    【STM32H7】第20章 ThreadX GUIX汉字显示(QSPI Flash全字库)
    【STM32H7】第19章 ThreadX GUIX的OLED单色屏移植
    【STM32F429】第19章 ThreadX GUIX的OLED单色屏移植
    第28届全球超顶级PCB设计PK结果公布,含炫酷PCB设计效果展示(2020-12-28)
    H7-TOOL固件升级至V1.45,增加上位机截图功能(2020-12-27)
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3167821.html
Copyright © 2011-2022 走看看