(7)风色从零单排《C++ Primer》 string
来源:互联网 发布:战地3低配优化 编辑:程序博客网 时间:2024/04/29 06:10
初始化:
string s1string s2(s1)string s2 = s1string s3("value")string s3 = "value"string s4(n,'c')
读取未知个数的字符串
1)cin
cin读取字符串时,遇到leading whitespace(eg spaces,newlines,tabs)就会完成一次读取。
string word;while(cin>>word) cout<<word<<endl;
2)getlin()
当我们想整行读取时,可以使用getline,当遇到换行时完成一次读取。
string line;while(getline(cin,line)) cout<<line<<endl;
string操作
1)enpty和size
empty()返回true,字符串是否为空
size()返回字符串个数,注意它的类型时一个unsigned类型,string::size_type因此,在比较时,要注意unsigned类型不要和负值比较。
auto len = line.size();//len has type string::size_type
==,当两个字符串有相同的字符内容时,返回真。
>,< 有三种情况
string str = "Hello";string phrase = "Hello World";string slang = "Hiya";
str<phrase , slang > phrase>str (i>e)
3)字符串相加
string s1 = "hello,", s2 = "world\n"string s3 = s1 +s2;//s3 is hello,world\nstring s4 = s1 + ",";//okstring s5 = "hello" + ",";//error: no string operandstring s6 = s1 + "," + "world";//okstring s7 = "hello" + "," + s2;//error can't add string literals
4)字符串访问
遍历每一个字符
for(auto c:str) cout << c << endl;
for(auto &c : s) c = toupper(c);
for(decltype(s.size())) index = 0;index != s.size() && !isspace(s[index]);++index) s[index] = toupper(s[index]);
const string hexdigits = "0123456789ABCDEF"string result;string::size_type n;while (cin >> n) if(n < hexdigits.size()) result += hexdigits[n];cout << result << endl;
cctype Functions
需要引入cctype库。
isalnum(c)
isalpha(c)
iscntrl(c)
isdigit(c)
islower(c)
isgraph(c)
isprint(c)
ispunct(c)
isspace(c)
isupper(c)
isxdigit(c)
tolower(c)
toupper(c)
0 0
- (7)风色从零单排《C++ Primer》 string
- (1)风色从零单排《C++ Primer》 一个简单的c++程序
- (4)风色从零单排《C++ Primer》 变量,引用,指针
- (2)风色从零单排《C++ Primer》 一个简单的书店程序
- (3)风色从零单排《C++ Primer》 重要的基本类型
- (5)风色从零单排《C++ Primer》 const,typedef,auto,decltype
- (6)风色从零单排《C++ Primer》 结构体,头文件
- 从零单排c++ primer(7)
- 从零单排7
- 从零单排c++ primer(1)
- 从零单排c++ primer(2)
- 从零单排c++ primer(3)
- 从零单排c++ primer(4)
- 从零单排c++ primer(5)
- 从零单排c++ primer(6)
- 从零单排c++ primer(8)
- 从零单排c++ primer(9)
- 从零单排c++ primer(10)
- ios8开发gps的小结
- plsql 结果集 中文乱码
- C++ 类继承与对象赋值 情况下 成员变量的覆盖 浅析
- linux下mysql数据库的导入导出
- SAT阅读:句子填空题解题指导
- (7)风色从零单排《C++ Primer》 string
- 网络基础(2)--通信过程之一HTTP协议
- Java中都通用文件下载(ContentType、文件头、response、out四步骤)
- Spring MVC 文件下载 - 兼容IE
- 【c语言】模拟实现strchr函数,功能:在一个字符串中查找一个字符第一次出现的位置,如果没有出现返回NULL
- Why No Guaranteed Delivery
- MFC 获取分辨率
- 降维系列算法【主成分分析】
- 甲骨文终获Java编程语言版权