STL六大组件:
容器
算法
配接器
迭代器
仿函数
空间配置器
温馨提示:只讲常用接口,使用方法说明详见代码注释
目录
一、string类对象的常见构造
二、string类对象的容量操作
三、类对象的访问及遍历操作
四、string类对象的修改操作
五、string类非成员函数
六、汇总代码
#include
using namespace std;
#include
//typedef basic_string u16string;std::string s0("Initial string");//构造空的string类对象,即空字符串(默认构造)
//default (1) explicit basic_string(const allocator_type& alloc = allocator_type());
std::string s1;//用C-string来构造string类对象
//copy(2) basic_string(const basic_string& str);
std::string s2(s0);//拷贝构造函数
//copy(2) basic_string(const basic_string& str);
std::string s2(s0);void Teststring()
{string s1;//构造空的string类对象s1string s2("hello sunlang");//用C格式字符串构造string类对象s2string s3(s2);//拷贝构造s3
}
//返回字符串有效字符长度
//size_type size() const;
int main()
{std::string str("Test string");std::cout << "The size of str is" << str.size() << "characters.\n";return 0;
}
//检测字符串释放为空串,是返回true,否则返回false
//bool empty() const;
int main()
{std::string content;std::string line;std::cout << "Please introduce a text.Enter an empty line to finish:\n";do{getline(std::cin, line);content += line + '\n';} while (!line.empty());std::cout << "The text you introduced was:\n" << content;return 0;
}
//清空有效字符
//void clear();
int main()
{char c;std::string str;std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";do {c = std::cin.get();str += c;if (c == '\n'){std::cout << str;str.clear();}} while (c != '.');return 0;
}
//为字符串预留空间**
//void reserve (size_type n = 0);
int main()
{std::string str;std::ifstream file("test.txt", std::ios::in | std::ios::ate);if (file) {std::ifstream::streampos filesize = file.tellg();str.reserve(filesize);file.seekg(0);while (!file.eof()){str += file.get();}std::cout << str;}return 0;
}
//将有效字符的个数改成n个,多出的空间用字符C填充
//void resize (size_type n);
//void resize(size_type n, charT c)
int main()
{std::string str("I like to code in C");std::cout << str << '\n';std::string::size_type sz = str.size();str.resize(sz + 2, '+');std::cout << str << '\n';str.resize(14);std::cout << str << '\n';return 0;
}
//返回pos位置的字符,const string类对象调用
//reference operator[] (size_type pos);
//const_reference operator[] (size_type pos) const;
int main()
{std::string str("Test string");for (int i = 0; i < str.length(); ++i){std::cout << str[i];}return 0;
}
//在字符串后面追加字符串str
//string(1)
//basic_string& operator+= (const basic_string& str);
//c - string(2)
//basic_string & operator+= (const charT * s);
//character(3)
//basic_string& operator+= (charT c);
int main()
{std::string name("John");std::string family("Smith");name += "K"; // c-stringname += family; // stringname += '\n'; // characterstd::cout << name;return 0;
}
//返回C格式字符串
//const charT* c_str() const;
int main()
{std::string str("Please split this sentence into tokens");char* cstr = new char[str.length() + 1];std::strcpy(cstr, str.c_str());// cstr now contains a c-string copy of strchar* p = std::strtok(cstr, " ");while (p != 0){std::cout << p << '\n';p = strtok(NULL, " ");}delete[] cstr;return 0;
}
//从字符串pos位置开始往后找字符C,返回该字符在字符串中的位置
//string(1)
//size_type find(const basic_string& str, size_type pos = 0) const;
//c - string(2)
//size_type find(const charT * s, size_type pos = 0) const;
//buffer(3)
//size_type find(const charT* s, size_type pos, size_type n) const;
//character(4)
//size_type find(charT c, size_type pos = 0) const;
int main()
{std::string str("There are two needles in this haystack with needles.");std::string str2("needle");// different member versions of find in the same order as above:std::string::size_type found = str.find(str2);if (found != std::string::npos)std::cout << "first 'needle' found at: " << found << '\n';found = str.find("needles are small", found + 1, 6);if (found != std::string::npos)std::cout << "second 'needle' found at: " << found << '\n';found = str.find("haystack");if (found != std::string::npos)std::cout << "'haystack' also found at: " << found << '\n';found = str.find('.');if (found != std::string::npos)std::cout << "Period found at: " << found << '\n';// let's replace the first needle:str.replace(str.find(str2), str2.length(), "preposition");std::cout << str << '\n';return 0;
}
//输入运算符重载
//template
//basic_istream& operator>> (basic_istream& is,
// basic_string& str);
int main()
{std::string name;std::cout << "Please, enter your name: ";std::cin >> name;std::cout << "Hello, " << name << "!\n";return 0;
}
//输出运算符重载
//template
//basic_ostream& operator<< (basic_ostream& os,
// const basic_string& str);
int main()
{std::string str = "Hello world!";std::cout << str << '\n';return 0;
}
//获取一行字符串
//template
//(1)basic_istream& getline(basic_istream& is,
// basic_string& str, charT delim);
//(2)
//template
//basic_istream& getline(basic_istream& is,
// basic_string& str);
int main()
{std::string name;std::cout << "Please, enter your full name: ";std::getline(std::cin, name);std::cout << "Hello, " << name << "!\n";return 0;
}
//大小比较
int main()
{std::string foo = "alpha";std::string bar = "beta";if (foo == bar) std::cout << "foo and bar are equal\n";if (foo != bar) std::cout << "foo and bar are not equal\n";if (foo < bar) std::cout << "foo is less than bar\n";if (foo > bar) std::cout << "foo is greater than bar\n";if (foo <= bar) std::cout << "foo is less than or equal to bar\n";if (foo >= bar) std::cout << "foo is greater than or equal to bar\n";return 0;
}
#include
using namespace std;
#include
//测试string容量相关的接口
//size/clear/resize
void Teststring1()
{//注意:string类对象支持直接用cin和cout进行输入和输出string s("hello,sunlang!!!");//用C-siring来构造string类对象cout << s.size() << endl;//返回字符串有效字符长度cout << s.length() << endl;//返回字符串有效字符长度cout << s.capacity() << endl;//返回空间总大小cout << s << endl;//将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小s.clear();cout << s.size() << endl;cout << s.capacity() << endl;//将s中有效字符个数增加到10个,多出位置用‘a'进行补充//"aaaaaaaa"s.resize(10, 'a');cout << s.size() << endl;cout << s << endl;cout << s.capacity() << endl;//将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行补充//"aaaaaaaa\0\0\0\0"s.resize(15);cout << s.size() << endl;cout << s.capacity() << endl;cout << s << endl;//将s中有效字符个数缩小到5个s.resize(5);cout << s.size() << endl;cout << s.capacity() << endl;cout << s << endl;
}
void Teststring2()
{string s;//测试reserve是否会改变string中有效元素个数s.reserve(100);cout << s.size() << endl;cout << s.capacity() << endl;//测试reserve参数小于string的底层空间大小时,是否会将空间缩小s.reserve(50);cout << s.size() << endl;cout<