📄 string_test.cpp
字号:
/**********************************************************************************
* *
* Copyright (c) by Royal. All rights reserved. *
* *
* Permission to use, copy, modify, and distribute this software for any purpose *
* is hereby granted without fee, provided that this copyright and permissions *
* notice appear in all copies and derivatives, and that no charge may be made *
* for the software and its documentation except to cover cost of distribution. *
* *
* This software is provided "as is" without express or implied warranty. *
* *
**********************************************************************************/
/*
* Description:
*
* Examples of std::string.
*
* History:
*
* Initial version created by Xyb, May, 2004.
*
* Notes:
*
* This code has been written to conform to standard C++ and STL. It has been
* compiled successfully using GNU C++ 3.2, Borland C++ 5.5, and Visual C++ 7.0.
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
// string有12种初始化方式
cout << endl << "string 有12种初始化方式: " << endl;
char* chars = "hello the world";
string s1;
string s2("hello the world");
string s3(chars);
string s4(s2);
string s5(s2, 6);
string s6(s2, 6, 3);
string s7("hello the world", 5); // 注意在参数含义上与s5(s2,5)的区别
string s8("hello the world", 6, 3);
string s9(10, 'a');
string s10(s2.begin() ,s2.end());
string s11 = s2;
string s12 = "hello";
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
cout << "s4: " << s4 << endl;
cout << "s5: " << s5 << endl;
cout << "s6: " << s6 << endl;
cout << "s7: " << s7 << endl;
cout << "s8: " << s8 << endl;
cout << "s9: " << s9 << endl;
cout << "s10: " << s10 << endl;
cout << "s11: " << s11 << endl;
cout << "s12: " << s12 << endl;
// size() && length() && capacity() && max_size() && empty()
cout << endl << "size() && length() && capacity() && max_size() && empty()" << endl;
cout << "s2.size(): " << s2.size() << endl;
cout << "s2.length(): " << s2.length() << endl;
cout << "s2.max_size(): " << s2.max_size() << endl;
cout << "s2.capacity(): " << s2.capacity() << endl;
cout << "s2.empty(): " << (s2.empty() ? "true" : "false") << endl;
// == && != && < && <= && > && >=
cout << endl << " == && != && < && <= && > && >= " << endl;
cout << "s2 == s3: " << ((s2 == s3) ? "true" : "false") << endl;
cout << "chars != s2: " << ((chars != s2) ? "true" : "false") << endl;
cout << "s2 < s5: " << ((s2 < s5) ? "true" : "false") << endl;
cout << "s5 > s6: " << ((s5 > s6) ? "true" : "false") << endl;
// compare()
// 0:相等 <0:小于 >0:大于
cout << endl << "compare():" << endl;
cout << "s2.compare(s3): " << s2.compare(s3) << endl;
cout << "s2.compare(s5): " << s2.compare(s5) << endl;
cout << "s5.compare(s2): " << s5.compare(s2) << endl;
cout << "s2.compare(chars):" << s2.compare(chars) << endl;
cout << "s2.compare(6, 9, s5): " << s2.compare(6, 9, s5) << endl;
cout << "s2.compare(6, 9, s3, 6, 9): " << s2.compare(6, 9, s3, 6, 9) << endl;
cout << "s2.compare(6, 9, chars): " << s2.compare(6, 9, chars) << endl;
cout << "s2.compare(0, 15, chars, 15): " << s2.compare(0, 15, chars, 15) << endl;
// operator[] && at()
cout << endl << "operator[] && at(): " << endl;
cout << "s2[0]: " << s2[0] << endl;
cout << "s2[1]: " << s2[1] << endl;
cout << "s2[14]: " << s2[14] << endl;
cout << "s2[15]: " << s2[15] << endl;//仅用于测试,s2.at(15)将会导致异常
cout << "s2.at(10): " << s2.at(10) << endl;
// c_str() && data() && copy()
cout << endl << "c_str() && data() && copy(): " << endl;
const char* cstr1;
cstr1 = s2.c_str();
cout << cstr1 << endl;
cstr1 = s2.data();
cout << cstr1 << endl;
char* cstr2 = new char[s2.size()];
s2.copy(cstr2, s2.size());
cout << cstr2 << endl;
delete[] cstr2;
// assign() && swap()
cout << endl << "assign() && swap(): " << endl;
string s14;
string s15;
string s16;
string s17;
string s18;
s14.assign(s2);
s15.assign(chars);
s16.assign(s2,6,3);
s17.assign(chars,10);
s18.assign(10,'b');
cout << "s14: " << s14 << endl;
cout << "s15: " << s15 << endl;
cout << "s16: " << s16 << endl;
cout << "s17: " << s17 << endl;
cout << "s18: " << s18 << endl;
s14.swap(s16);
cout << "s14[after s14.swap(s16) ]: " << s14 << endl;
cout << "s16[after s14.swap(s16) ]: " << s16 << endl;
// append() && += && push_back()
cout << endl << "append() && += && push_back(): " << endl;
cout << "s2.append(s5): " << s2.append(s5) << endl;
cout << "s2.append(s2,0,5): " << s2.append(s2,0,5) << endl;
cout << "s2.append(\"hi\"): " << s2.append(" hi") << endl;
cout << "s2.append(\"the string\",3): " << s2.append("the string", 3) << endl;
cout << "s2.append(3, 'c'): " << s2.append(3,'c') << endl;
cout << "s2.append(s5.begin(), s5.end()): " << s2.append(s5.begin(), s5.end()) << endl;
cout << "s3 += s5: " << (s3 += s5) <<endl;
cout << "s3 += \" hihi\": " << (s3 += " hihi") << endl;
cout << "s3 += \'c\': " << (s3 += 'c') << endl;
s3.push_back('d');
cout << "s3[after s3.push_back('d')]: " << s3 << endl;
// insert()
cout << endl << "insert():" << endl;
cout << "s4.insert(5, s5): " << s4.insert(5,s5) << endl;
cout << "s4.insert(5, s5, 0, 3): " << s4.insert(5, s5, 0, 3) << endl;
cout << "s4.insert(5, \"aaa\"): " << s4.insert(5, "aaa") << endl;
cout << "s4.insert(5, \"how are you\", 3): " << s4.insert(5, "how are you", 3) << endl;
cout << "s4.insert(5, 3, \'e\'): " << s4.insert(5, 3, 'e') << endl;
s4.insert(++s4.begin(), 3, 'f');
cout << "s4[after s4.insert(++s4.begin(), 5, \'f\')]: " << s4 << endl;
s4.insert(++s4.begin(), 'g');
cout << "s4[after s4.insert(++s4.begin(),'g')]: " << s4 <<endl ;
s4.insert(++s4.begin(), s5.begin(), s5.end());
cout << "s4[after s4.insert(++s4.begin(), s5.begin(), s5.end())]: " << s4 << endl;
// erase() && resize() && clear()
cout << endl << "erase() && resize() && clear(): " << endl;
s4.erase(30);
cout << "s4[after s4.erase(30)]: " << s4 << endl;
s4.erase(20, 5);
cout << "s4[after s4.erase(20, 5)]: " << s4 << endl;
s4.erase(s4.begin());
cout << "s4[after s4.erase(s4.begin())]: " << s4 << endl;
s4.erase(++s4.begin(), --s4.end());
cout << "s4[after s4.erase(++s4.begin(), --s4.end())]: " << s4 << endl;
s4.resize(10);
cout << "s4[after s4.resize(10)]: " << s4 << endl;
s4.resize(15, 'h');
cout << "s4[after s4.resize(15, 'h')]: " << s4 << endl;
s4.clear();
cout << "s4[after s4.clear()]: " << s4 << endl;
// replace()
cout << endl << "replace(): " << endl;
cout << "s2: " << s2 << endl;
cout << "s2.replace(5, 10, s5): " << s2.replace(5, 10, s5) << endl;
cout << "s2.replace(s2.begin(), s2.end(), s5): " << s2.replace(s2.begin(), s2.end(), s5) << endl;
cout << "s2.replace(0, 3, s5, 4, 5): " << s2.replace(0, 3, s5, 4, 5) << endl;
cout << "s2.replace(0, 5, \"hello\", 5): " << s2.replace(0, 5, "hello", 5) << endl;
cout << "s2.replace(s2.begin(), s2.end(), \"hello\", 5): " << s2.replace(s2.begin(), s2.end(), "hello", 5) << endl;
cout << "s2.replace(0, 5, 10, \'a\'): " << s2.replace(0, 5, 10, 'a') << endl;
cout << "s2.replace(s2.begin(), s2.end(), 5, \'b\'): " << s2.replace(s2.begin(), s2.end(), 5, 'b') << endl;
cout << "s2.replace(s2.begin(), s2.end(), s5.begin(), s5.end()): " << s2.replace(s2.begin(), s2.end(), s5.begin(), s5.end()) << endl;
// find() && rfind()
// rfind()与find()十分类似,不再举例
cout << endl << "find() && rfind(): " << endl;
string::size_type pos;
cout << "s3: " << s3 << endl;
pos = s3.find('e');
cout << "letter e' position in s3 is: " << pos <<endl;
pos = s3.find('e', 10);
cout << "an e' position in s3 is: " << pos << endl;
pos = s3.find("the");
cout << "\"the\" position in s3 is: " << pos << endl;
pos = s3.find("the", 10);
cout << "a \"the\" position in s3 is: " << pos << endl;
pos = s3.find("the", 5);
cout << "the position in s3 is: " << pos << endl;
// find_first_of() && find_first_not_of() && find_last_of() && find_last_not_of()
// find_last与find_first十分类似,不再举例.
cout << endl << "find_first_of() && find_first_not_of() && find_last_of() && find_last_not_of(): " << endl;
s2 = "abcde";
s3 = "cdefg";
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
pos = s2.find_first_of(s3);
cout << "第一个既在s3中出现也在s2中出现的字母在s2中的位置: " << pos << endl;
pos = s2.find_first_not_of(s3);
cout << "第一个未在s3中出现但在s2中出现的字母在s2中的位置: " << pos << endl;
// substr()
cout << endl << "substr(): " << endl;
cout << "s10: " << s10 << endl;
cout << "s10.substr(6): " << s10.substr(5) << endl;
cout << "s10.substr(6, 3): " << s10.substr(6, 3) << endl;
// +
cout << endl << "+ :" << endl;
s2 = s3 + "tom";
cout << s2 << endl;
s2 = s3 + s5;
cout << s2 << endl;
s2 = s2 + 'h';
cout << s2 << endl;
// 关于 string::npos 常量
cout << endl << "关于 npos 常量" << endl;
cout << "s2: " << s2 << endl;
string::size_type pos1;// 注意:此处pos1不要用其他类型
pos1 = s2.find('z');
if (pos1 != string::npos ) // 一定要使用string::size_type型的变量与string::npos进行比较
{
cout << "'z' could find in s2." << endl;
}
else
{
cout << "'z' could not find in s2." << endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -