一、wchar_t类型
char类型变量可以存储一个字节的字符,它用来保存英文字符和标点符号是可以的,但是对于汉字、韩文以及日文这样的字符却不可以,因为汉字、韩文以及日文每一个文字都占据两个字节,为了解决这个问题,c++提出了wchar_t类型,称之为双字节类型,又称宽字符类型。
int main(int argc, wchar_t* argv[])
{
//使用setlocale函数将本机的语言设置为中文简体
setlocale(LC_ALL,"chs");//LC_ALL表示设置所有的选项(包括金融货币、小数点,时间日期格式、语言字符串的使用习惯等),chs表示中文简体
wchar_t wt[] = L"中国你好!";//大写字母L告诉编译器为"中"字分配两个字节的空间
wcout<<wt<<endl;//使用wcout来代替cout输出宽字符,wcin类代替cin输入宽字符
cout<<wcslen(wt)<<endl;//wcslen输出宽字符串的长度,输出长度是5
cout<<sizeof(wt)<<endl;//输出长度是12个字节,最后的wchar_t类型的'\0'两个字节
return 0;
}
二、c++容器中resize( )和reserve( )的区别
了解这两个函数的区别,首先要搞清楚容器的capacity(容量)与size(长度)的区别。size指容器当前拥有的元素个数;而capacity则指容器在必须分配新存储空间之前可以存储的元素总数。也可以说是预分配存储空间的大小。resize( )改变容器的size,reserve( )改变容器的capacity。
- resize(n)
调整容器的长度大小,使其能容纳n个元素。如果n小于容器的当前的size,则删除多出来的元素。否则,添加采用值初始化的元素。 - resize(n,t)
多一个参数t,将所有新添加的元素初始化为t。 - reserve(n)
预分配n个元素的存储空间。
三、find() & find_first_of
以string为例:
// Find sub-string in string
// Searches the string for the first occurrence of the sequence specified by its arguments.
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;
string.find(str)是在sting中找到完全匹配str的字符串,pos指定从string的哪个下表开始找。
// Find character in string
// Searches the string for the first character that matches any of the characters specified in its arguments.
size_t find_first_of (const string& str, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos, size_t n) const;
size_t find_first_of (char c, size_t pos = 0) const;
string.find_first_of(str) 是在string中找str中的任一字符第一次出现的位置。