C++ string类
来源:互联网 发布:黑帽子数据 编辑:程序博客网 时间:2024/04/30 08:18
class std::String
String class
typedef basic_string string
std::string是basic_string模板类的使用char作为类型的类实例,这种类处理字节与其所使用的编码无关:如果处理的是多字节可变长度的字符(比如UTF-8),该类中所有的成员(比如长度和大小),以及迭代器依然会按照字节来操作。
typedef basic_string string
成员函数
- 构造函数
- 析构函数
- operator=
迭代器
对应iterator: begin end 对应reverse iterator rbegin rend 对应const iterator cbegin cend 对应const_reverse_iterator crbegin crend
capacity
- size
- length
- max_size
- resize
- capacity
- reverse
- clear
- empty
- shirink_to_fit
元素读取
- operator[]
- at
- back
- front
字符串编辑器
- operator+=
- append
- push_back append character
- assign assign content to string
- insert
- erase
- replace
- swap
- pop_back delete last characters
字符串操作
- c_str
- data
- get_allocator
- copy
- find
- rfind 找到字符最后一次出现的位置
- find_first_of
- find_last_of
- find_first_not_of
- find_last_not_of
- substr
- compare
成员常量
npos size_t所能取到的最大值
static const size_t npos =- 1
函数重载
- operator+=
- relation operators 例如== <= >= !=等等
- swap 交换string对象的值
- operator>>字符串提取符
- operator<<字符串插入符
- getline 从输入流中提取一行形成string对象
进一步探讨string类成员函数
data()和c_str
const char* data( ) const;const char* c_str() const;
string类中间可能带’\0’;
string::c_str带’\0’,而string::data不带’\0’;
不要试图通过c_str()和data()返回指针来改变string内容;
assign()替换当前string 内容
string& assign ( const string& str );string& assign ( const string&, size_t pos, size_t n );string& assign ( const char* s, size_t n );string& assign ( const char* s );string& assign ( size_t n, char c );template <class InputIterator> string& assign ( InputIterator first, InputIterator last );
erase() erases a part of the string contents
string& erase ( size_t pos = 0, size_t n = npos );iterator erase ( iterator position );iterator erase ( iterator first, iterator last );
push_back() 添加单独一个字符到string的内容中
// string::push_back#include <iostream>#include <fstream>#include <string>using namespace std;int main (){ string str; ifstream file ("test.txt",ios::in); while (!file.eof()) { str.push_back(file.get()); } cout << str; return 0;}
capacity()容量/reverse()预留空间/resize()
size_t capacity ( ) const;//Returns the size of the allocated storage space in the string object.void reserve ( size_t res_arg=0 );//Request a change in capacity 重新分配容量给string对象void resize ( size_t n, char c );//c是填补用的字符void resize ( size_t n );//Resizes the string content to n characte.如果n>std::max_size 会抛出异常
copy()/replace()/insert()
size_t copy ( char* s, size_t n, size_t pos = 0) const;string& replace ( size_t pos1, size_t n1, const string& str );string& replace ( iterator i1, iterator i2, const string& str );string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 );string& replace ( size_t pos1, size_t n1, const char* s, size_t n2 );string& replace ( iterator i1, iterator i2, const char* s, size_t n2 );string& replace ( size_t pos1, size_t n1, const char* s );string& replace ( iterator i1, iterator i2, const char* s );string& replace ( size_t pos1, size_t n1, size_t n2, char c );string& replace ( iterator i1, iterator i2, size_t n2, char c );template<class InputIterator> string& replace ( iterator i1, iterator i2, InputIterator j1, InputIterator j2 ); string& insert ( size_t pos1, const string& str ); string& insert ( size_t pos1, const string& str, size_t pos2, size_t n ); string& insert ( size_t pos1, const char* s, size_t n); string& insert ( size_t pos1, const char* s ); string& insert ( size_t pos1, size_t n, char c );iterator insert ( iterator p, char c ); void insert ( iterator p, size_t n, char c );template<class InputIterator> void insert ( iterator p, InputIterator first, InputIterator last );
find()/rfind()/find_first_of()/find_last_of()/find_first_not_of()/find_last_not_of()
size_t find ( const string& str, size_t pos = 0 ) const;size_t find ( const char* s, size_t pos, size_t n ) const;size_t find ( const char* s, size_t pos = 0 ) const;size_t find ( char c, size_t pos = 0 ) const; //rfind()是找到字符串最后一次出现的位置// string::find#include <iostream>#include <string>using namespace std;int main (){ string str ("There are two needles in this haystack with needles."); string str2 ("needle"); size_t found; // different member versions of find in the same order as above: found=str.find(str2); if (found!=string::npos) cout << "first 'needle' found at: " << int(found) << endl; found=str.find("needles are small",found+1,6); if (found!=string::npos) cout << "second 'needle' found at: " << int(found) << endl; found=str.find("haystack"); if (found!=string::npos) cout << "'haystack' also found at: " << int(found) << endl; found=str.find('.'); if (found!=string::npos) cout << "Period found at: " << int(found) << endl; // let's replace the first needle: str.replace(str.find(str2),str2.length(),"preposition"); cout << str << endl; return 0;}----------size_t find_last_of ( const string& str, size_t pos = npos ) const;size_t find_last_of ( const char* s, size_t pos, size_t n ) const;size_t find_last_of ( const char* s, size_t pos = npos ) const;size_t find_last_of ( char c, size_t pos = npos ) const;/* * 从string对象内容的结尾开始找出匹配属于c,str或者string的任意其中 *一部分最后一次出现的位置 */ // string::find_last_of#include <iostream>#include <string>using namespace std;void SplitFilename (const string& str){ size_t found; cout << "Splitting: " << str << endl; found=str.find_last_of("/\\"); cout << " folder: " << str.substr(0,found) << endl; cout << " file: " << str.substr(found+1) << endl;}int main (){ string str1 ("/usr/bin/man"); string str2 ("c:\\windows\\winhelp.exe"); SplitFilename (str1); SplitFilename (str2); return 0;}//find_first_of()和find_first_not_of()/find_last_not_of()与find_last_of()类似
0 0
- 【C/C++】String类实现
- [C++]STL string类
- C++string类
- C++string类总结
- c++_10: string类
- 【C++】String类
- C++--string类
- string类(c++)
- c++string类
- C++string类
- 【C++】string 类
- C++—String类
- C++String类
- C++string类
- 浅谈C++string类
- c++string类
- [C++]String类的实现
- C++string类常用函数
- Selenium私房菜系列6 -- 深入了解Selenium RC工作原理(1)
- JAVA POST GET
- 客户端Crash一般原因小结
- 25匹马5个跑道问题
- Android正式签名打包的过程
- C++ string类
- matlab中关于转到二值化图像的问题
- Java学习之旅_基础篇_05
- Selenium私房菜系列7 -- 深入了解Selenium RC工作原理(2)
- shirt----spring security的另外一个选贤
- 「专治不明觉厉」之“大数据”
- acm hdu p1040 As Easy As A+B 冒泡升序排列
- DataSupport.order 里变量名要与类里面的大小写保持一致,如submitTime 不能写为 submittime
- Java-instanceof总结