c++ primer(第五版)笔记 第三章(1)string类

来源:互联网 发布:俄航中转莫斯科 知乎 编辑:程序博客网 时间:2024/04/29 10:04
//#define NDEBUG#include<iostream>#include<string>#include<cassert>//using namespace std;//using 声明不应该出现在头文件中,会使每个包含头文件的文件都会有这个声明,造成名字冲突using std::cout;using std::cin;using std::endl;using std::string;void string_learn();void practice_3_6();void practice_3_7();void practice_3_10();int main(){//string_learn();practice_3_6();practice_3_7();practice_3_10();return 0;}void string_learn(){//string 构造函数//(1) Default constructor. Constructs empty string (zero size and unspecified capacity)//默认构造函数,构造空字符串(0大小,未指定容量)//------------------------------------------------------------------------------//explicit basic_string();  //------------------------------------------------------------------------------string s;assert( s.empty() && ( s.length() == 0) && ( s.size() == 0));//(2) Constructs the string with count copies of character ch.The behavior is undefined if count >= npos//用 count 个字符 ch 构造一个字符串,如果 count 大于 npos, 行为未定义//npos 是 string 类中定义的一个保证大于任何有效下标的值,类型 string::size_type( unsigned int), 大于该值意思就是超出范围的无效下标//------------------------------------------------------------------------------//basic_string( size_type count, CharT ch);//------------------------------------------------------------------------------string s2(5, '*');cout << string::npos << endl;cout << "s2:" << s2 << endl;//(3) Constructs the string with a substring [pos, pos+count) of other. If the requested substring lasts past the end of the string, or if count == npos, the resulting substring is [pos, size())//用字符串 other 的 pos 到 ( pos + count - 1 ) 位构造字符串,如果请求的子串超出了原字符串,或者 count 大于 npos, 结果为 pos 到 ( size() - 1 ) 位, pos 从 0 开始计算//------------------------------------------------------------------------------//basic_string(const basic_string& other, size_type pos, size_type count = std::basic_string::npos);//------------------------------------------------------------------------------string const sTmp("string test!");string s3(sTmp, 2);cout << "s3:" <<  s3 << endl;//(4) Constructs the string with the first count characters of character string pointed to by s. s can contain null characters. The behavior is undefined if s does not point at an array of at least count elements of CharT//用c风格字符串指针 s 指向的字符串的前 count 个字符构造字符串,s 可以包含空白字符, 如果 s 指向的字符串长度不足 count,其行为是未定义的//------------------------------------------------------------------------------//basic_string( const CharT* s, size_type count)//------------------------------------------------------------------------------string s4("hello world", 2);cout << "s4:" << s4 << endl;//(5) Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by s. The length of the string is determined by the first null character. The behavior is undefined if s does not point at an array of at least Traits::length(s)+1 elements of CharT.//用c风格字符串的指针 s 所指向的一个以空白符结束的字符串的副本构造字符串,字符串的长度由第一个空字符结束('\0')何时出现决定,如果 s 指向的字符串长度不足 Traits::length(s)+1,其行为是未定义的//------------------------------------------------------------------------------//basic_string( const CharT* s )//------------------------------------------------------------------------------string s5("hello\0world");cout << "s5:" << s5 << endl;//(6) Constructs the string with the contents of the range [first, last). This constructor does not participate in overload resolution if InputIt does not satisfy InputIterator.//用迭代器 first 到 last - 2 表示范围,注意 end() 返回下一个位置的指针//------------------------------------------------------------------------------//template< class InputIt >//basic_string( InputIt first, InputIt last)//------------------------------------------------------------------------------char charTmp[] = "HELLO WORLD";string s6(std::begin(charTmp) + 3, std::end(charTmp));cout << "s6:" << s6 << "!" << endl;//(7) Copy constructor. Constructs the string with the copy of the contents of other//拷贝构造函数,用另一个 string 对象的副本构造//------------------------------------------------------------------------------//basic_string( const basic_string& other )//------------------------------------------------------------------------------string s7(sTmp);cout << "s7:" << s7 << endl;//(8) Move constructor. Constructs the string with the contents of other using move semantics. other is left in valid, but unspecified state.//移动构造函数(待完善)//------------------------------------------------------------------------------//basic_string( basic_string&& other ) noexcept  (since C++11)//------------------------------------------------------------------------------string s8(string("hello ") + string("world"));cout << "s8:" << s8 << endl;//(9) Constructs the string with the contents of the initializer list init.//用初始化列表构造//------------------------------------------------------------------------------//basic_string( std::initializer_list<CharT> init)  (since C++11)//------------------------------------------------------------------------------string s9({ 'h', 'e', 'l', 'l', 'o' });cout << "s9:" << s9 << endl;//IOcin >> s >> s9; //以第一个不为空的字符开始,到第一个空白处结束cout << s << " - "<< s9 << endl;//使用 getline 读取一行,直到遇到换行符,读入换行符,但不存入到 string 对象中while (getline(cin, s))cout << s << endl;//string::size_type类型,由 size() 返回的无符号整型而且可以放下任何 string 对象的大小//string 类定义的配套类型,体现了标准库类型与机器无关的特性//注意在表达式中与带符号数一起将产生意想不到的结果//用 == 和 != 检测2个 string 对象是否相等,相等意味着它们的长度相同而且包含的字符也全都相同//关系运算符的比较规则://1.如果2个 string 对象的长度不相同,而且较短的 string 对象的每个字符都与较长 string 对象对应位置上的字符相同,就说较短 string 对象小于较长string 对象//2.如果2个 string,结果由2者第一对相异字符的字典顺序决定//可以使用 = 将一个 string 对象赋给另一个string str(10, 'x');string s10 = str;//可以使用 + 拼接2个 string 对象,或者拼接1个 string 对象和1个字符(串)字面值,当把 string 对象和字符字面值及字符串字面值混在一条语句中时,必须确保每个 + 符号的两侧运算对象有一个是 string 对象string s11 = "hello", s12 = "world", s13;//s13 = "hello" + "world"; 错误,不能把字面值直接相加s13 = s11 + " " + s12 + '\n';//关于字符特性的函数,定义于 cctype//isalnum(c): checks if a character is alphanumeric//isalpha(c): checks if a character is alphabetic//islower(c): checks if a character is lowercase//isupper(c): checks if a character is an uppercase character//isdigit(c): checks if a character is a digit//isxdigit(c): checks if a character is a hexadecimal character//iscntrl(c): checks if a character is a control character//isgraph(c): checks if a character is a graphical character//isspace(c): checks if a character is a space character//isblank(c): checks if a character is a blank character (C++11)//isprint(c): checks if a character is a printing character//ispunct(c): checks if a character is a punctuation character//tolower(c): converts a character to lowercase//toupper(c): converts a character to uppercase//范围 for (range for)//语法: for (declaration : expression) {}//下标运算符[],接收 string::size_type 类型的值(下标或索引),表示要访问的字符的位置,返回值是该位置上字符的引用//下标从 0 开始,必须大于等于 0,且小于 size(),超出此范围的下标将引发不可预知的结果}void practice_3_6(){string str("hello world");for (auto &c : str)c = 'X';cout << str << endl;}void practice_3_7(){string str("hello world");for (char &c : str)c = 'X';cout << str << endl;}void practice_3_10(){string str, sTmp;cin >> str;for (auto &c : str)if (!ispunct(c))sTmp += c;cout << sTmp << endl;}

0 0
原创粉丝点击