一只小蒟蒻备考蓝桥杯的日志
参考 C++中map的用法总结
#include
形式:
map<键,键值>mp;
以下以 map
mp['c']++;
for(auto& it:mp) {
cout << it.first << " " << it.second << endl; // c 1
}
auto it = mp.find('a');
if(it != mp.end()) {
cout << "Find it!" << endl;
} else {
cout << "Didn't find it..." << endl;
}
刪除了返回1,反之返回0
int flag = mp.erase('c');
cout << flag << endl; // 1
cout << mp.size() << endl; // 0
参考 C++:cin、cin.getline()、getline()的用法
参考 使用C语言gets函数和gets_s函数
gets()
可以无限读取,不会判断上限,以回车结束读取——不安全
C标准的最新修订版(2011年)已明确将该功能从其规范中删除
(所以说,不要用了吧~)
gets_s()
由于gets_s是Visual Studio 编译器提供的安全gets函数,因此仅在该编译器环境底下可以使用
(局限性,那也不要用了)
接收一个字符串,可以接收空格并输出
#include
string str;
getline(cin,str);
cout<<str<<endl;
当同时使用cin>>,getline()时,需要注意的是,在cin>>输入流完成之后,getline()之前,需要通过
str="\n";
getline(cin,str);
的方式将回车符作为输入流cin以清除缓存,如果不这样做的话,在控制台上就不会出现getline()的输入提示,而直接跳过,因为程序默认地将之前的变量作为输入流。 (直接摘录自参考,不太理解)
参考 获取C/C++字符串、字符数组长度
char str1[10];
int size = sizeof(str1) / sizeof(str1[0]);
cout << size << endl; // 10
char str2[10] = "123";
cout << strlen(str2) << endl; // 3
string str3 = "1234";
cout << str3.length() << endl; // 4
#include
string str1 = "12";
string str2 = "34";
string str3 = "56";
string str = str1 + str2 + str3;
cout << str << endl; // 123456
但是! string str = “12” + “34” + “56” 会报错
参考 解决c++中 “” “” 拼接
错误案例如下:
处理方法:显式声明其中一个为std::string类型
string str = "12" + string("34");
to_string() 一下 (int -> string)
int x = 12;
string str1 = "34";
string str = to_string(x) + str1;
cout << str << endl; // 1234
补充知识点 string -> int
atoi + str.c_str()
string str = "12";
int num = atoi(str.c_str());
题做得很少…知识倒学了很多(扶额苦笑)
进度要加快了!
加油! ! !