C++ push_back,back,删除元素之pop_back(),erase(),remove()

参考:https://blog.csdn.net/qq_32867925/article/details/104851126
https://blog.csdn.net/dongyanxia1000/article/details/52838922
按顺序存入容器用push_back();
取最后一个元素用pop_back();
获取最后一个元素back

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
	vector<int> vec;
	int sum = 0, i = 0;
	vec.push_back(10);
	vec.push_back(20);
	vec.push_back(30);
	cout << "init vec.size() =  "<< vec.size() << endl;
	while(!vec.empty())
	{
		i++;
		sum += vec.back();	//取最后一个元素	
		vec.pop_back();         //删除最后一个元素
		printf("after %d time, sum: %d size = %d\n", i, sum, vec.size());		
	}
	system("pause");
	return 0;
}
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
	vector<int> vec;
	for( int i = 0; i < 10; i++ )
	{
		vec.push_back( i );
	}
	printf("init vec size: %d\n", vec.size());   //0-9
	
	vec.erase( vec.begin() + 5 );                //删除第6个元素,{0 1 2 3 4 6 7 8 9}
	printf("after erase size: %d\n", vec.size());//9
	vec.erase( vec.begin(), vec.begin() + 3 );   //删除第1个元素,到第4个元素 {3 4 6 7 8 9}
	printf("vec = { ");
	for( unsigned int j = 0; j < vec.size(); j++ )
	{
		
		printf("%d ", vec[j]);
	}
	printf("}\n");
	
	system("pause");
	return 0;
}
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
	vector<int> vec;
	vec.push_back(100);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(500);
	cout << &vec << endl;
	vector<int>::iterator itor;
	for( itor = vec.begin(); itor != vec.end(); itor++ )
	{
		if( *itor == 300 )
		{
			itor = vec.erase(itor);
		}
	}
 
	for( itor = vec.begin(); itor != vec.end(); itor++)
	{
		cout<< *itor <<" ";
	}	
	system("pause");
	return 0;
}
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <stdio.h>
using namespace std;
 
void Show(int v)
{
	std::cout << v << ' ';
}
 
int main()
{
        int age[10] ={ 2, 3, 5, 4, 2, 3, 2, 6, 2, 7 };
	std::list<int> list_a(age, age + 10);
	std::list<int> list_b(list_a);
	printf("Original list_a:\n");
	for_each(list_a.begin(), list_a.end(), Show);
	printf("\n\n");
	list_a.remove( 2 );
	printf("remove() method list_a:\n");
	for_each(list_a.begin(), list_a.end(), Show);
	printf("\n\n");
	list<int>::iterator itor;
	itor = remove(list_b.begin(), list_b.end(), 2);
	printf("remove() function list_b:\n");
	for_each(list_b.begin(), list_b.end(), Show);
	printf("\n\n");
	list_b.erase( itor, list_b.end() );
	printf("erase() method list_b:\n");
	for_each(list_b.begin(), list_b.end(), Show);
	printf("\n");
	system("pause");
	return 0;
}
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <stdio.h>
using namespace std;
 
void Show(int v)
{
	std::cout << v << ' ';
}
 
int main()
{
	int age[10] ={ 2, 3, 5, 4, 2, 3, 2, 6, 2, 7 };
	vector<int> vec_a(age, age + 10);
	printf("Original vec_a:\n");
	for_each(vec_a.begin(), vec_a.end(), Show);
	printf("\n\n");
	
	vector<int>::iterator itor;
	itor = remove(vec_a.begin(), vec_a.end(), 2);
	printf("remove() function vec_a:\n");
	for_each(vec_a.begin(), vec_a.end(), Show);
	printf("\n");
	printf("remove() size: %d\n\n", vec_a.size());
	vec_a.erase( itor, vec_a.end() );
	printf("erase() method vec_a:\n");
	for_each(vec_a.begin(), vec_a.end(), Show);
	printf("\n");
	printf("erase() size: %d\n\n", vec_a.size());
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值