C++及C中的 string char指標及char陣列
阿新 • • 發佈:2019-01-02
之所以拋棄char*的字串而選用C++標準程式庫中的string類,是因為他和前者比較起來,不必擔心記憶體是否足夠、字串長度等等,而且作為一個類出現,他整合的操作函式足以完成我們大多數情況下的需要.
下面我們首先從一些示例開始學習下string類的使用.
1)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s("hehe");
cout<<s<<endl;
cin.get();
}
2)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs);
cout<<s<<endl;
cin.get();
}
3)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs,1,3); //指定從chs的索引1開始,最後複製3個位元組
cout<<s<<endl;
cin.get();
}
4)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1("hehe");
string s2(s1);
cout<<s2<<endl;
cin.get();
}
5)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1("hehe",2,3);
string s2(s1);
cout<<s2<<endl;
cin.get();
}
6)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs,3); //將chs前3個字元作為初值構造
cout<<s<<endl;
cin.get();
}
7)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s(10,'k'); //分配10個字元,初值都是'k'
cout<<s<<endl;
cin.get();
}
//以上是string類例項的構造手段,都很簡單.
9)
//賦新值
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s(10,'k'); //分配10個字元,初值都是'k'
cout<<s<<endl;
s = "hehehehe";
cout<<s<<endl;
s.assign("kdje");
cout<<s<<endl;
s.assign("fkdhfkdfd",5); //重新分配指定字串的前5的元素內容
cout<<s<<endl;
cin.get();
}
10)
//swap方法交換
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1 = "hehe";
string s2 = "gagaga";
cout<<"s1 : "<<s1<<endl;
cout<<"s2 : "<<s2<<endl;
s1.swap(s2);
cout<<"s1 : "<<s1<<endl;
cout<<"s2 : "<<s2<<endl;
cin.get();
}
11)
//+=,append(),push_back()在尾部新增字元
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "hehe";
s += "gaga";
cout<<s<<endl;
s.append("嘿嘿"); //append()方法可以新增字串
cout<<s<<endl;
s.push_back('k'); //push_back()方法只能新增一個字元...
cout<<s<<endl;
cin.get();
}
12)
//insert() 插入字元.其實,insert運用好,與其他的插入操作是一樣的.
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "hehe";
s.insert(0,"頭部"); //在頭部插入
s.insert(s.size(),"尾部"); //在尾部插入
s.insert(s.size()/2,"中間");//在中間插入
cout<<s<<endl;
cin.get();
}
13)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
s.erase(0,1); //從索引0到索引1,即刪除掉了'a'
cout<<s<<endl;
//其實,還可以使用replace方法來執行刪除操作
s.replace(2,3,"");//即將指定範圍內的字元替換成"",即變相刪除了
cout<<s<<endl;
cin.get();
}
14)
//clear() 刪除全部字元
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.length()<<endl;
s.clear();
cout<<s.length()<<endl;
//使用earse方法變相全刪除
s = "dkjfd";
cout<<s.length()<<endl;
s.erase(0,s.length());
cout<<s.length()<<endl;
cin.get();
}
15)
//replace() 替換字元
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
s.replace(2,3,"!!!!!");//從索引2開始3個位元組的字元全替換成"!!!!!"
cout<<s<<endl;
cin.get();
}
16)
//==,!=,<,<=,>,>=,compare() 比較字串
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1 = "abcdefg";
string s2 = "abcdefg";
if (s1==s2)cout<<"s1 == s2"<<endl;
else cout<<"s1 != s2"<<endl;
if (s1!=s2)cout<<"s1 != s2"<<endl;
else cout<<"s1 == s2"<<endl;
if (s1>s2)cout<<"s1 > s2"<<endl;
else cout<<"s1 <= s2"<<endl;
if (s1<=s2)cout<<"s1 <= s2"<<endl;
else cout<<"s1 > s2"<<endl;
cin.get();
}
17)
//size(),length() 返回字元數量
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.size()<<endl;
cout<<s.length()<<endl;
cin.get();
}
18)
//max_size() 返回字元的可能最大個數
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.max_size()<<endl;
cin.get();
}
19)
//empty() 判斷字串是否為空
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s ;
if (s.empty())
cout<<"s 為空."<<endl;
else
cout<<"s 不為空."<<endl;
s = s + "abcdefg";
if (s.empty())
cout<<"s 為空."<<endl;
else
cout<<"s 不為空."<<endl;
cin.get();
}
20)
// [ ], at() 存取單一字元
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
cout<<"use []:"<<endl;
for(int i=0; i<s.length(); i++)
{
cout<<s[i]<<endl;
}
cout<<endl;
cout<<"use at():"<<endl;
for(int i=0; i<s.length(); i++)
{
cout<<s.at(i)<<endl;
}
cout<<endl;
cin.get();
}
21)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
const char * chs1 = s.c_str();
const char * chs2 = s.data();
cout<<"use at():"<<endl;
int i;
for(i=0; i<s.length(); i++)
{
cout<<"c_str() : "<<chs1[i]<<endl;
cout<<"data() : "<<chs2[i]<<endl;
}
cout<<"c_str() : "<<chs1<<endl;
cout<<"data() : "<<chs2<<endl;
cout<<endl;
cin.get();
}
22)
// substr() 返回某個子字串
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
string str = s.substr(5,3);//從索引5開始3個位元組
cout<<str<<endl;
cin.get();
}
23)
// find 查詢函式
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
string pattern = "fg";
string::size_type pos;
pos = s.find(pattern,0); //從索引0開始,查詢符合字串"f"的頭索引
cout<<pos<<endl;
string str = s.substr(pos,pattern.size());
cout<<str<<endl;
cin.get();
}
24)
// begin() end() 提供類似STL的迭代器支援
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
{
cout<<*iter<<endl;
}
cout<<endl;
cin.get();
}
下面我們首先從一些示例開始學習下string類的使用.
1)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s("hehe");
cout<<s<<endl;
cin.get();
}
2)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs);
cout<<s<<endl;
cin.get();
}
3)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs,1,3); //指定從chs的索引1開始,最後複製3個位元組
cout<<s<<endl;
cin.get();
}
4)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1("hehe");
string s2(s1);
cout<<s2<<endl;
cin.get();
}
5)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1("hehe",2,3);
string s2(s1);
cout<<s2<<endl;
cin.get();
}
6)
#include <string>
#include <iostream>
using namespace std;
void main()
{
char chs[] = "hehe";
string s(chs,3); //將chs前3個字元作為初值構造
cout<<s<<endl;
cin.get();
}
7)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s(10,'k'); //分配10個字元,初值都是'k'
cout<<s<<endl;
cin.get();
}
//以上是string類例項的構造手段,都很簡單.
9)
//賦新值
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s(10,'k'); //分配10個字元,初值都是'k'
cout<<s<<endl;
s = "hehehehe";
cout<<s<<endl;
s.assign("kdje");
cout<<s<<endl;
s.assign("fkdhfkdfd",5); //重新分配指定字串的前5的元素內容
cout<<s<<endl;
cin.get();
}
10)
//swap方法交換
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1 = "hehe";
string s2 = "gagaga";
cout<<"s1 : "<<s1<<endl;
cout<<"s2 : "<<s2<<endl;
s1.swap(s2);
cout<<"s1 : "<<s1<<endl;
cout<<"s2 : "<<s2<<endl;
cin.get();
}
11)
//+=,append(),push_back()在尾部新增字元
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "hehe";
s += "gaga";
cout<<s<<endl;
s.append("嘿嘿"); //append()方法可以新增字串
cout<<s<<endl;
s.push_back('k'); //push_back()方法只能新增一個字元...
cout<<s<<endl;
cin.get();
}
12)
//insert() 插入字元.其實,insert運用好,與其他的插入操作是一樣的.
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "hehe";
s.insert(0,"頭部"); //在頭部插入
s.insert(s.size(),"尾部"); //在尾部插入
s.insert(s.size()/2,"中間");//在中間插入
cout<<s<<endl;
cin.get();
}
13)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
s.erase(0,1); //從索引0到索引1,即刪除掉了'a'
cout<<s<<endl;
//其實,還可以使用replace方法來執行刪除操作
s.replace(2,3,"");//即將指定範圍內的字元替換成"",即變相刪除了
cout<<s<<endl;
cin.get();
}
14)
//clear() 刪除全部字元
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.length()<<endl;
s.clear();
cout<<s.length()<<endl;
//使用earse方法變相全刪除
s = "dkjfd";
cout<<s.length()<<endl;
s.erase(0,s.length());
cout<<s.length()<<endl;
cin.get();
}
15)
//replace() 替換字元
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
s.replace(2,3,"!!!!!");//從索引2開始3個位元組的字元全替換成"!!!!!"
cout<<s<<endl;
cin.get();
}
16)
//==,!=,<,<=,>,>=,compare() 比較字串
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s1 = "abcdefg";
string s2 = "abcdefg";
if (s1==s2)cout<<"s1 == s2"<<endl;
else cout<<"s1 != s2"<<endl;
if (s1!=s2)cout<<"s1 != s2"<<endl;
else cout<<"s1 == s2"<<endl;
if (s1>s2)cout<<"s1 > s2"<<endl;
else cout<<"s1 <= s2"<<endl;
if (s1<=s2)cout<<"s1 <= s2"<<endl;
else cout<<"s1 > s2"<<endl;
cin.get();
}
17)
//size(),length() 返回字元數量
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.size()<<endl;
cout<<s.length()<<endl;
cin.get();
}
18)
//max_size() 返回字元的可能最大個數
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg";
cout<<s.max_size()<<endl;
cin.get();
}
19)
//empty() 判斷字串是否為空
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s ;
if (s.empty())
cout<<"s 為空."<<endl;
else
cout<<"s 不為空."<<endl;
s = s + "abcdefg";
if (s.empty())
cout<<"s 為空."<<endl;
else
cout<<"s 不為空."<<endl;
cin.get();
}
20)
// [ ], at() 存取單一字元
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
cout<<"use []:"<<endl;
for(int i=0; i<s.length(); i++)
{
cout<<s[i]<<endl;
}
cout<<endl;
cout<<"use at():"<<endl;
for(int i=0; i<s.length(); i++)
{
cout<<s.at(i)<<endl;
}
cout<<endl;
cin.get();
}
21)
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
const char * chs1 = s.c_str();
const char * chs2 = s.data();
cout<<"use at():"<<endl;
int i;
for(i=0; i<s.length(); i++)
{
cout<<"c_str() : "<<chs1[i]<<endl;
cout<<"data() : "<<chs2[i]<<endl;
}
cout<<"c_str() : "<<chs1<<endl;
cout<<"data() : "<<chs2<<endl;
cout<<endl;
cin.get();
}
22)
// substr() 返回某個子字串
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
string str = s.substr(5,3);//從索引5開始3個位元組
cout<<str<<endl;
cin.get();
}
23)
// find 查詢函式
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
string pattern = "fg";
string::size_type pos;
pos = s.find(pattern,0); //從索引0開始,查詢符合字串"f"的頭索引
cout<<pos<<endl;
string str = s.substr(pos,pattern.size());
cout<<str<<endl;
cin.get();
}
24)
// begin() end() 提供類似STL的迭代器支援
#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "abcdefg1111";
for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
{
cout<<*iter<<endl;
}
cout<<endl;
cin.get();
}