stl string是真正的字串。
#include #include #include int main()
c語言風格的複製。
const char* pasconststr = "hello c";
char *pascopy = new char(strlen(pasconststr)+1);
strcopy(pascopy,pasconststr);
delete pascopy;
c++字串輸出
#include#include#include#includeusing namespace std;
int main()
cout << strstlstring << endl;//這樣可以全部輸出
cout << "使用迭代器來操作字串中的每乙個字元" << endl;
string::const_iterator itr;
for (itr = strstlstring.begin();//begin()是乙個方法
itr != strstlstring.end();
++itr)//迭代器是乙個指標
cout << "使用新的for迴圈格式迴圈輸出" << endl;
for (auto x : strstlstring)
cout << "把c++的字串變成c風格的字串" << endl;
cout << strstlstring.c_str() << endl;//把c++的字串變成c風格的字串
return 0;
}
c++字串拼接
#include#include#include#includeusing namespace std;
int main()
c++字串的查詢
#include#include#include#includeusing namespace std;
int main()
else
//找到全部的day字串
size_t nsubstringoffset = strsample.find("day",0);
while (nsubstringoffset !=string::npos)
cout << "查詢字元" << endl;
size_t nsearchoffset = strsample.find('a', 0);
while (nsearchoffset != string::npos)
return 0;
}
c++字串截短
#include#include #include#include#includeusing namespace std;
int main()
cout << strsample << endl;
return 0;
}
字串演算法,字串翻轉,大小寫轉換
#include#include #include#include#includeusing namespace std;
int main()
String用法(STL基本容器)
string c 標準庫中的string表示可變長的字串,它在標頭檔案string裡面。include string using std string 直接初始化和拷貝初始化 string s1 初始化字串,空字串 string s2 s1 拷貝初始化,深拷貝字串 string s3 i am b ...
STL容器操作 string詳解
1 string概念 string和char 的比較 string封裝了char 管理這個字串,是乙個char 型的容器。string管理char 所分配的記憶體。每一次string的複製,取值都由string類負責維護,不用擔心複製越界和取值越界等。查詢find,拷貝copy,刪除erase,替換...
STL容器 Vector用法
標頭檔案 include 建立 vectorv 尾部插入元素 v.push back a 在第i 1個元素前面插入a v.insert v,begin i 刪除第i 1個元素 v.erase v,begin i 刪除區間 i,j 的元素 v.erase v.begin i,v.end j 下標訪問第...