c++(STL中一些常用操作)

#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
using namespace std;
 
int main(){
    
    
	string s{
    
    "aawdwwd"};
//	s.erase(1,3); // 第一个参数指定从此索引位置开始删除,第二个元素指定删除的长度,如果无则会删除从索引对应的元素之后的所有元素 
//	cout << s; 
	vector<int> v1{
    
    1,2,3,4,1,2,3,4,1,2,3,4};
//	for(auto &i:s){
    
    
//		i = toupper(i); // 范围for使字母大写 
//		cout << i;
//	}
//	cout << s[2];   // string类型可以利用下标访问 
//	sort(s.rbegin(),s.rend());  // 反向迭代器,降序; 
//	s.push_back('6');   // string类型字符串尾部插入指定字符 
//	s.append("989");  // string类型字符串尾部追加指定字符串 
//	cout << v1.front() << v1.back();  // 输出容器的首个元素和尾部元素 
//	for(auto i=v1.rbegin();i!=v1.rend();i++){ //反向迭代器 
//		cout << *i;
//	}
//	v1.insert(v1.begin()+1,8);    //在第二个元素前插入8 
//	v1.resize(20,8);   // 重置v1的大小为20,多余部分用8填充 
//	int x = count(s.begin(),s.end(),'a');  //查找某元素在容器中出现的次数
//	cout << x;
//	int y = count(v1.begin(),v1.end(),1);
//	cout << y;
// 关联容器 c.find(k);返回迭代器  c.count(k); c.insert(k); 
//  *****关联容器可以使用范围for;  set,map都可以用迭代器; 
	auto i = find(v1.begin(),v1.end(),1); //返回查找到的出现首此的迭代器
									//未查找到时返回v1.end()迭代器 
//	v1.erase(i);	 // 删除容器中迭代器指向的元素 
//	v1.erase(v1.begin(),v1.begin()+3); // 删除指定位置的元素(string类也可以) 
//	cout << s.find('a'); // string类的额外操作,查找指定元素出现的首位置的索引 
//	cout << s.rfind('a'); // 查找指定元素最后出现的位置的索引
//	v1.clear();     // 清除容器v1中所有元素  
//	for(auto &i:s){
    
    
//		cout << i;
//	}
	map<string,int> m={
    
    {
    
    "aa",1}}; 
//	m["b"] = 2;  // 添加指定键值对 
//	m.insert({"c",3}); // 添加指定键值对
//	m.erase("aa");    // 删除指定键对应的元素 
//	m.erase(m.begin());   // 删除指定迭代器对应的元素 
//	for(auto iv=m.begin();iv!=m.end();iv++)
//	{
    
    
//	   cout << iv->first << ":" << iv->second; //(迭代器)对应的元素的键:值 
//	}
//	set<int> set1{1,2,2,3}; // 会自动过滤重复的元素 
//	set1.insert(4);   // 添加元素 
//	for(auto i=set1.begin();i!=set1.end();i++){
    
    
//		cout << *i << " " ;
//	}
//	auto f = [](int x,int y)->int{return x+y;}; // lambda 表达式 
//	cout << f(1,5); 
	for(auto i:m){
    
    
		cout << i.first << i.second;
	} 
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_46456049/article/details/108587832