1.string初始化方法:
string s1 = "abcdefg"; //初始化方式1
string s2("abcdefg"); //初始化方式2
string s3 = s2; //通过拷贝构造函数 初始化s3
string s4(7,'s'); //初始化7个s的字符串
2.codeBlocks如果要调试二维数组那么应该确定二维数组的长度,如写成a[m][n],不能查看数组元素,需用常量固定长度,如a[2][3];
3.vector两层需要注意不能直接赋值,比如
vector<vector<string>> v;
v[0].push_back(...)
因为这时候v的第一层是还没有给内存的,应该
vector<string> v1;
v.push_back(v1);
4.string类型的substr()一定注意和java中的不同,第一个参数为首地址,第二个参数为长度而不是尾地址
5.如果需要用printf输出string类型,只需要对string类型使用c_str()即可转换C++字符串标准形式,不能直接输出
string a="ab";
printf("%s",a.c_str());
6.用printf输出百分号需要写%%
7.atoi atof是c语言提供的一个扩展功能,它能将一个字符串转换成对应的整型和字符串类型,使用这些函数时,必须引入头文件#include <stdlib.h>
,并且需要将string类型使用c_str()转换成C++标准字符串类型
8.C++中字符串分两种,一种是C语言的字符串,一种是string字符串。C语言字符串是不可以直接比较大小的,string是可以直接比较大小的。要注意的是java中写compare函数会用string 的compareTo来比较string并返回结果,但C++不行,因为如果字符串相等,会返回string::npo,可以直接用大于号小于号来比较
9.list:push_back()从集合最后插入元素
push_front()从集合前面插入元素
list的一个优势就是支持从开头插入元素,vector就不具备这个特性
10.遍历list集合可以采用迭代器,如:
list<int> integers;
integers.push_back(1);
integers.push_back(1);
integers.push_back(1);
integers.push_back(1);
integers.push_back(1);
list<int>::interator itor;
itor=integers.begin();
while(itor++!=itegers.end()){
cout<<*itor<<endl;
}
迭代器可以理解成是一个指针对象,所以如果是结构体对象,遍历输出应该写成itor->…
11.在使用cin后如果需要使用getline需要处理掉cin缓存区中剩下的结束符,也就是接着cin后的第一次getline得到的结果一定是空,cin和getline区别就是:
getline()中的结束符,结束后,结束符不放入缓存区;
cin的结束符,结束后,结束符还在缓存区;例如:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1;
int x;cin>>x;
getline(cin,str1);
while(x--){
getline(cin,str1);
cout<<str1<<"\n";
}
return 0;
}