《c++ primer》-——1 標準庫string型別
1.名稱空間的using宣告:
之前在編寫程式時使用std空間中的定義,採用的是
std::cin>>a;
std::cout<<a;
作用域限定符::的含義是右運算元可以在左運算元中找到定義.
C++提供了更為方便的方法使用名稱空間的內容,using宣告。
Using name_space::name;
後面在使用的時候,可以直接引用名字,而不需要再加上名稱空間。
1.1 每個名字都需要一個using宣告:
只要使用到std空間中的成員,每個成員都要using宣告,每次using宣告只能宣告一個成員。
using std::cin; using std::cout; using std::endl;
1.2 在標頭檔案中必須採用完全限定的名稱空間成員的引用:
就是說,在標頭檔案中不要使用using宣告,而要使用name_space::name的形式。原因是,標頭檔案會被不同的檔案include,如果使用using宣告,那麼每個檔案中都使用了該using宣告,然而對某些檔案來說有些宣告並不是必要的,甚至會造成命名衝突。
2.標準庫string型別:
String型別用來處理長度可變的字串,c++標準庫來進行記憶體的管理和儲存的分配,以及提供一些可執行的操作。
String型別包含在string標頭檔案,在std空間中有定義,所以在使用時:
#include <string> using std::string;
2.1 string型別變數的定義和初始化
String是一個標準庫中的類型別,所以它的初始化必須使用建構函式(constructor function),預設時呼叫預設建構函式(default constructor function).
幾種初始化的方式:
string s1;
string s2(“hello world”);//字串字面值初始化
string s3(s1);//s1的副本初始化
string s4(10,’a’);//10個字元’a’進行初始化
string s5=s2;//先是呼叫預設建構函式初始化s5,然後用s2進行賦值
注意字串字面值與string型別是不同的!下面測試測試字串字面值“hello world”與string型別的區別:
#include <iostream>
#include <string>
#include <cstdlib>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string s("hello world");
cout << sizeof(s) << endl << sizeof("hello world") << endl;//本人機器的運算結果分別為28和12
getchar();
return 0;
}
2.2 string型別變數的輸入輸出:
string型別的變數同其他內建型別類似。
2.2.1 cin從非空白字元開始讀取,到空白字元讀取結束。空白字元包括(空格、回車、製表符等)
cin>>s;
//你輸入的內容為 hello world
cout<<s;
//那麼標準輸出列印的為hello
2.2.2 getline讀取一行資料,getline接受兩個引數,一個輸入流物件,一個string物件,遇到回車時讀取結束,getline函式返回值將回車符扔掉
getline(cin,s);
//此時你輸入 hello world
cout<<s;
//那麼標準輸出列印位 hello world
2.2.3 輸入未知數目的string物件
While(cin>>s)
{
……
}
While(getline(cin,s))
{
……
}
>>運算子返回的是左運算元的物件,若返回的輸入流物件合法,則繼續while迴圈,如返回物件不合法或是讀取到檔案結尾,則終止迴圈。
Windows下的檔案結束符(end of file) ctrl+z
Linux下的檔案結束符 Ctrl+d
2.3 String物件的操作
2.3.1 string的size和empty操作
成員函式size()返回字串中字元的個數:
int main()
{
<span style="white-space:pre"> </span>string s("hello ");
<span style="white-space:pre"> </span>cout << s.size() << endl;//返回字串中字元的個數,結果為6,包括空格在內
<span style="white-space:pre"> </span>getchar();
<span style="white-space:pre"> </span>return 0;
}
判斷字串是否為空:
int main()
{
string s;
if (s.size() == 0)
{
cout << "empty" << endl;
}
if (s.empty())
{
cout << "empty" << endl;
}
getchar();
return 0;
}
2.3.2 string::size_type
我們知道,string類類成員函式size()的返回值是一個unsigned型別,但事實上size()的返回值是size_type型別。
int,long等一些整數型別表示的範圍是與機器平臺有關的,不同的平臺表示的範圍不同,如果將size()的返回值定義為int型別的話,在進行平臺移植的時候可能會出現錯誤,所以string類型別定義了一些配套型別,這樣就能與機器無關了。size_type就是一種配套型別,它與unsigned具有相同的含義,並且可以保證表示任意string物件的長度
size_type是由string定義的,所以在使用時
string::size_type;
2.3.3 string關係操作符
string變數進行比較時,大寫字母在小寫字母前面,按照字典序進行比較
== <= >= != < >
2.3.4 string物件的賦值
string s1,s2=”hello world”;
s1=s2;
string物件在進行賦值時會遇到效率的問題,就上例而言,在s2賦值給s1的過程中,先是s1釋放記憶體空間,然後分配給s1足夠存放s2副本內容的記憶體空間,最後把s2中的字元複製到新分配的s1記憶體空間。
2.3.5 string物件相加
string s1(“hello ”);
string s2(“world\n”);
string s3=s1+s2;//s3="hello world\n"
s1+=s2;//s1="hello world\n"
2.3.6 和字串字面值的連線
int main()
{
string s1("hello");
string s2("world\n");
string s3 = s1 + " " + s2;
cout << s3;
getchar();
return 0;
}
+操作符的左右運算元中必須有一個是string物件
int main()
{
string s1 = "hello";
string s2 = "world";
string s3 = s1 + ",";
// string s4 = "hello" + ",";
string s5 = s1 + "," + "world";
//string s6 = "hello" + "," + s2;
return 0;
}
對於s5:string標準庫定義加操作返回一個string物件,這樣s1+”,”返回一個string物件,再和”world”進行連線。(類似於輸入輸出運算子的串聯效果)。
顯然s6是不合法的。
2.3.7 從string物件獲取字元
string物件可以通過下標運算子[]來訪問string物件中的單個字元,下標是一個size_type 型別的值,從0開始,最大為size()-1。
int main()
{
string s("hello world");
for (string::size_type index = 0; index != s.size(); ++index)
{
cout << s[index];
}
cout << endl;
getchar();
return 0;
}
注意不要出現下標越界
2.3.8 下標操作可用左值
int main()
{
string s("hello world");
for (string::size_type index = 0; index != s.size(); ++index)
{
s[index] = '*';
cout << s[index];
}
getchar();
return 0;
}
2.3.9 計算下標值
任何可產生的整型表示式都可做下標,但索引的實際資料確是unsigned型別的string::size_type,注意不要越界。
2.4 string物件中字元的處理
一些字元處理函式:
Isalnum(c) // 如果c是字母或數字,則為true
Isalpha(c) //如果c是字母,則為true
Isdigit(c) //如果c是數字,則為true
Iscntrl(c) //如果c是控制字元,則為true //在ascii碼中,0-31號及127號為控制字元
Islower(c) //如果是小寫字母,則為true
Isupper(c) //如果是大寫字母,則為true
Isgraph(c) //如果不是空格,但可列印,則為true
Tolower(c) //如果c不是小寫字母,則返回其小寫形式,否則返回c
Toupper(c) //如果c不是大寫字母,則返回其大寫形式,否則返回c
Isxdigit(c) //如果c是16進位制數,則返回true
Isspace(c) //如果c是空白字元,則返回true
Ispunct(c) //如果c是標點符號,則為true
Isprint(c) //如果c是可列印的字元,則為true
上述字元處理函式都包含在<cctype>標頭檔案中
int main()
{
/*計算字串中標點符號的數目*/
string s("[email protected]$%hello");
string::size_type count = 0;//最好用這種配套型別,易於跨平臺維護
for (string::size_type index = 0; index != s.size(); ++index)
{
if (ispunct(s[index]))
{
++count;
}
}
cout << count << endl;
getchar();
return 0;
}
int main()
{
/*convert s to uppercase*/
string s("hello world\n");
for (string::size_type index = 0; index != s.size(); ++index)
{
s[index] = toupper(s[index]);
}
cout << s;
getchar();
return 0;
}
相關推薦
【C++ Primer】標準庫string型別
/* *標準庫string型別 *Zhi-Yun Deng *2013-11-01 */ #include <iostream> #include <string> //標準庫string型別標頭檔案 #include <cctype>
《c++ primer》-——1 標準庫string型別
1.名稱空間的using宣告: 之前在編寫程式時使用std空間中的定義,採用的是 std::cin>>a; std::cout<<a; 作用域限定符::的含義是右運算元可以
第三章 標準庫型別(part 1) 標準庫 string 型別
除基本資料型別外,C++ 還定義了一個內容豐富的抽象資料型別標準庫。 其中最重要的標準庫型別是 string 和 vector,它們分別定義了大小可變的字串和集合。 string 和 vector 往往將迭代器用作配套型別(companion type),用於訪問strin
【C++ Primer】標準庫bitset型別
/* *標準庫bitset型別 *Zhi-Yun Deng *2013-10-31 */ #include <iostream> #include <bitset> //標準庫bitset型別標頭檔案 #include <string>
C++ Primer 第三章 標準庫string型別
標準庫string型別: string型別支援長度可變的字串. #include<string> using std::string; 1.string物件的定義和初始化 string s1; //預設建構函式,s1為空串 string s2(s1);
C++ primer筆記 -標準庫型別
最重要的兩個標準庫型別:string和vector string 型別的輸入操作符: 1、讀取並忽略開頭所有的空白符 2、讀取字元直至再次遇到空白字元,讀取終止 string物件的基本操作: string s; s.empty() 如果為空返回true,不為空返回fals
C++ 標準庫 string型別(二)
C++ 標準庫 string型別(一) 一,string物件的操作 1,s.size() s.size()是描述string字元多少的,如下: #include<iostream> #i
標準庫string型別
與其他標準庫型別一樣,使用者程式要使用string型別物件,必須包含相關標頭檔案。 1.string物件初始化和定義 string常用的建構函式
3.2 標準庫string型別
本章大意 講述了標準庫string型別的相關知識細節摘錄 1. 字串字面值和string型別是不同的型別,這點要明確。別讓它們相加。 2. string物件定義了許多比較實用的操作。比如emptysize等。 3. 儲存string的size操作結果的變數必須為string
標準庫string型別及其實現原理
1 string簡介string型別支援長度可變的字串,C++標準庫將負責管理與儲存字串相關的記憶體,以及提供各種有用的操作。2 string標頭檔案#include<string> #using std:string;3 string 操作3.1 string初
《隨筆十八》—— C++中的 “ 標準庫型別string ”
目錄 初始化 string 物件的方式 string 物件的操作 cin 和 getline 讀寫 string物件 string 的 size ()操作 和 string::size_type 型別 比較string物件
c++基礎五---標準庫型別(string ,vector)
string型別 支援長度可變的字串 因為歷史原因以及為了與C語言相容,字串字面值與標準庫string型別不是同一種類型。這一點很容易引起混亂。 s.empty () 如果s為空串,則返回ture s.size() s[n] 返回s中位置為n的字元 size(
C++ Primer 第三章 標準庫vector型別
vector是同一種類型的物件的集合,每個物件都有一個對應的整數索引值。 使用vector之前,必須包含相應的標頭檔案。 #include<vector> using std::vector; vector不是一種資料型別,而只是一個類模板,可用來定義任意多種資
C++中的標準庫型別——string
string 標準庫型別string是一種可變長的字元序列。string定義在std名稱空間中。在使用string前,需要: #include<string> using std
C 標準庫 - string.h之strncpy使用
填充 reat 函數 clas != count imu serve uno strncpy 把 src 所指向的字符串復制到 dest,最多復制 n 個字符。當 src 的長度小於 n 時,dest 的剩余部分將用空字節填充。 char *strncpy(char
C 標準庫 - string.h之strcat使用
www. href 產生 rmi put for turned med main strcat Appends a copy of the source string to the destination string. The terminating null cha
C 標準庫 - string.h之strlen使用
rac ati oob [] mine eat including 定義 mina strlen Returns the length of the C string str. The length of a C string is determined by the
C 標準庫 - string.h之memcpy使用
uno over character copies www. zhang 別名 .com val memcpy Copy block of memory Copies the values of num bytes from the location pointed t
<C++學習十一>標準庫string的使用(未完待續)
clu size_t ... namespace art ring star using start 使用: 1、C++標準庫負責管理和存儲字符串所占用的內存; 2、頭文件:#include<string> 3、空間域:using namespa
C標準庫string.h原始碼三strcat/strncat
/* char *strcat(dst, src) - concatenate (append) one string to another Purpose: Concatenates src onto the end of dest. Assumes enough