//#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;
//IO
cin >> 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;
}
c++ primer(第五版)筆記 第三章(1)string類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.