C++之string
来源:互联网 发布:mac将文件夹移动硬盘 编辑:程序博客网 时间:2024/06/01 08:32
头文件 #include<string>
支持长度可变的字符串。
1、string对象的定义和初始化
string s1; //默认构造函数,s1为空串string s2(s1); //将s2初始化为s1的一个副本string s3("value"); //将s3初始化为一个字符串字面值副本string s4(n, 'c'); //将s4初始化为字符'c'的n个副本2、string对象的读写
int main(){ string s; cin >> s; cout << s << endl; return 0;}⚠️:string类型的输入操作符:
读取并忽略开头所有的空白字符(如空格、换行符、制表符);
读取字符直至再次遇到空白字符,读取终止。
用get line读取整行文本:
int main() { string s; while( get line(cin, s) ) { cout << s << endl; }}
换行符将不会存储在string对象中
3、string iterators
- begin
- Return iterator to beginning (public member function )
- end
- Return iterator to end (public member function )
- rbegin
- Return reverse iterator to reverse beginning (public member function )
- rend
- Return reverse iterator to reverse end (public member function )
- cbegin
- Return const_iterator to beginning (public member function )
- cend
- Return const_iterator to end (public member function )
- crbegin
- Return const_reverse_iterator to reverse beginning (public member function )
- crend
- Return const_reverse_iterator to reverse end (public member function )
4、string Capacity:
- size
- Return length of string (public member function )
- length
- Return length of string (public member function )
- max_size
- Return maximum size of string (public member function )
- resize
- Resize string (public member function )
- capacity
- Return size of allocated storage (public member function )
- reserve
- Request a change in capacity (public member function )
- clear
- Clear string (public member function )
- empty
- Test if string is empty (public member function )
- shrink_to_fit
- Shrink to fit (public member function )
5、Element access:
- operator[]
- Get character of string (public member function )
- at
- Get character in string (public member function )
- back
- Access last character (public member function )
- front
- Access first character (public member function )
6、Modifiers:
- operator+=
- Append to string (public member function )
- append
- Append to string (public member function )
- push_back
- Append character to string (public member function )
- assign
- Assign content to string (public member function )
- insert
- Insert into string (public member function )
- erase
- Erase characters from string (public member function )
- replace
- Replace portion of string (public member function )
- swap
- Swap string values (public member function )
- pop_back
- Delete last character (public member function )
- c_str
- Get C string equivalent (public member function )
- data
- Get string data (public member function )
- get_allocator
- Get allocator (public member function )
- copy
- Copy sequence of characters from string (public member function )
- find
- Find content in string (public member function )
- rfind
- Find last occurrence of content in string (public member function )
- find_first_of
- Find character in string (public member function )
- find_last_of
- Find character in string from the end (public member function )
- find_first_not_of
- Find absence of character in string (public member function )
- find_last_not_of
- Find non-matching character in string from the end (public member function )
- substr
- Generate substring (public member function )
- compare
- Compare strings (public member function )
8、Non-member function overloads
- operator+
- Concatenate strings (function )
- relational operators
- Relational operators for string (function )
- swap
- Exchanges the values of two strings (function )
- operator>>
- Extract string from stream (function )
- operator<<
- Insert string into stream (function )
- getline
- Get line from stream into string (function )
9、Member constants
- npos
- Maximum value for size_t (public static member constant )
10、对单个字符的处理
isalnum(c); //如果c为字母或者数字,就返回trueisalpha(c); //如果c为字母,则返回trueisdigit(c); //数字islower(c); //小写字母isupper(c); //大写字母isspace(c); //空白字符tolower(c); //如果是大写字母,返回其小写形式,否则直接返回ctoupper(c); //如果是小写字母,返回其大写字母形式,否则直接返回cisxdigit(c); //十六进制
0 0
- C++STL之string
- C++STL之string
- C++STL之string
- C++STL之string
- c++primer之string
- c++STL之string
- C++STL之string
- C++STL之string
- C++STL之string
- C++STL之string
- C++STL之string
- C++STL之string
- 【C++】C++之string
- C++STL之string
- C++STL之string
- c++stl之string
- C++STL之string上
- C++STL之string下
- C++之operator关键字(重载操作符) 使用总结
- C++实验3-本月有几天
- JavaScript常用API合集
- C++第三次实验:税收计算
- 全新的 flow.ci Dashboard UI 上线
- C++之string
- 支付宝报错
- nginx如何处理TCP/UDP session
- Android 优雅地退出App
- C/C++中生成随机序列——随机函数的选择和自我实现
- SQLite Xerial Driver
- POJ
- C++作业3
- LintCode 排序列表转换为二分查找树