十二月七日

天梯:
https://www.liuchuo.net/archives/5080
L1-048 矩阵A乘以B-PAT团体程序设计天梯赛GPLT
https://www.liuchuo.net/archives/5082
L1-049 天梯赛座位分配-PAT团体程序设计天梯赛GPLT
https://www.liuchuo.net/archives/5070
L1-043 阅览室-PAT团体程序设计天梯赛GPLT
https://www.liuchuo.net/archives/3827
CCCC-GPLT L1-039. 古风排版 团体程序设计天梯赛

1、auto的用法

 #include <iostream>
    #include <map>
    #include <set>
    using namespace std;
    int main()
    {
    	int n;cin>>n;
    	map<string,int>map;
    	set< string>name;
    	string s;
    	while(n--)
    	{
    		cin>>s;
    		name.insert(s);
    	}
    	cin>>n;
    	double num=0;
    	for(int i=0;i<n;i++)
    	{
    		int x;
    		cin>>s>>x;
    		num+=x;
    		map[s]=x;
    	}
    	num/=n;
    	for(auto it :map)
    	{
    		 if(name.find(it->first)==name.end()&&(it->first)>num)
    		 cout<<it->first<<endl;
    	} 
std::vector<int> vec;

或者

for(auto &p:vec)
 {
     cout<<p<<endl;
      //do something
 }
 for(auto &it :map)
{
	 if(name.find(it.first)==name.end()&&(it.second)>num)
	 cout<<it.first<<endl;
} 

遍历

需要改变迭代对象 for(auto &i:s)

string s = “hello”;
for (auto &i : s )
i = toupper(i); //改变成大写,影响s的值
cout<<s<<endl; //s的值是 HELLO1234

不需要改变迭代对象 for(auto i:s)

string s = “hello”;
for (auto i : s )
i = toupper(i); //改变成大写,不影响s的值
cout<<s<<endl; //s的值是 hello1234

迭代map

#include < iostream>
#include < map>

using namespace std;

int main() {
    map<int,string> student;
    student.insert(pair<int,string>(2,"li"));
    student.insert(pair<int,string>(1,"wang"));
    student.insert(pair<int,string>(3,"sun"));
    for(auto &v : student) // for(auto v : student)也是可以的
        cout<<"key: "<<v.first<<" | value: "<<v.second<<endl;
    return 0;
}1234567891011121314

更多

https://blog.csdn.net/huang_xw/article/details/8760403
https://blog.csdn.net/DLite/article/details/7658293

猜你喜欢

转载自blog.csdn.net/weixin_43719397/article/details/84885819
今日推荐