C++ String類

1、設計String類

//C++ 設計String類:構造函數,拷貝構造函數,析構函數,賦值函數

#include<iostream>
using namespace std;

class String
{
public:
	String(const char *str=NULL);
	String(const String&another);
	~String();
	String&operator=(const String& rhs);
private:
	char* m_data;
};

String::String(const char *str)
{
	if(NULL==str)
	{
		m_data=new char[1];
		m_data[0]='\0';
	}
	else
	{
		m_data=new char[strlen(str)+1];
		strcpy(m_data,str);
	}
}

String::String(const String&another)
{
	m_data=new char[strlen(another.m_data)+1];
	strcpy(m_data,another.m_data);
}

String::~String()
{
	delete []m_data;
}

String &String::operator =(const String &rhs)
{
	if(this==&rhs)
		return *this;
	delete []m_data;
	m_data=new char[strlen(rhs.m_data)+1];
	strcpy(m_data,rhs.m_data);
	return *this;
}

void main()
{
}


2、append()

//C++ append()函數|C++ 一個字符串連接在另一個字符串後面【C++ string】

#include<iostream>

#include<string>

using namespace std;

void main()

{

string a="www.ok2002.com";

string b=" study C++ program";

a.append(b,0,sizeof(b)+2);

cout<<a<<endl;

}

當程序運行的時候,上面的代碼將執行輸


3、assign()


//C++ assign()函數|C++ 從第二個字符開始保留6個字符,其它字符刪去【C++ string】
#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a,b="123456789";
	a.assign(b,2,6);//從第二個字符開始保留6個字符,其它字符刪去
	cout<<a<<endl;//輸出:345678
}



4、at()

//C++ 更安全的向量元素訪問方式 | C++ at()成員函數【C++ 用at()成員函數訪問向量元素,是一種安全的訪問方式,可以有效地避免錯誤代碼的繼續執行】

//以下程序使用at()成員函數訪問向量元素,是一種安全的訪問方式,可以有效地避免錯誤代碼的繼續執行
#include<iostream>
#include<string>
#include<vector>
using namespace std;

void main()
{
	int n[]={1,2,3};
	vector<int>a(n,n+sizeof(n)/sizeof(int));

	//錯誤的代碼:這裏試途訪問不存在的第四個和第五個元素,
	for(int i=0;i<5;i++)
	{
		cout<<a.at(i)<<' ';//當程序試途訪問第四個元素時,系統彈出錯誤提示對話框,並停止往下執行
		//cout<<a[i]<<' ';//不安全的訪問方法,
	}
	cout<<endl;
}
 


5、C++capacity() | C++ reserve()

//C++capacity() | C++ reserve()

#include<iostream>
#include<string>
#include<vector>
using namespace std;

void show(vector<int>rs)
{
	vector<int>::iterator u;
	for(u=rs.begin();u!=rs.end();u++)
		cout<<*u<<' ';
	cout<<endl;
}

void main()
{
	vector<int>a(10);
	cout<<a.capacity()<<endl;//向量a的空間大小:10
	show(a);//輸出10個元素,第個元素都是0

	vector<int>b;
	b.reserve(20);//分配空間
	cout<<b.capacity()<<endl; //向量b的空間大小:20
	show(b);//?????爲什麼只輸出一個換行,其它什麼都沒有呢?
}


6、clear()

//C++ clear()清空元素| C++ 刪除鏈表全部元素

#include<iostream>
//#include<string>
//#include<vector>
#include<list>
using namespace std;

void show(list<int>rs)
{
	list<int>::iterator u;
	for(u=rs.begin();u!=rs.end();u++)
		cout<<*u<<' ';
	cout<<endl;
}

void main()
{
	list<int>a;
	a.push_back(1);
	a.push_back(2);
	a.push_back(3);
	show(a);//輸出元素:1 2 3 並且換行
	a.clear();//清空所有元素/刪除鏈表全部元素
	show(a);//輸出換行,沒有元素
}


7、compare() 

//C++ compare() | C++ 兩個字符串相同時返回0 | C++ 與長度比它短的字符串相比較時返回1| C++ 與長度比它長的字符串相比較時返回-1 

#include<iostream>
#include<string>
//#include<vector>
//#include<list>
using namespace std;

void main()
{
	string a="ok2002.com";
	string b="2002";
	string c="ww11ok15011com";

	cout<<a.compare(b)<<endl;//與長度比它短的字符串相比較時返回1
	cout<<a.compare(c)<<endl;//與長度比它長的字符串相比較時返回-1

	cout<<b.compare(a)<<endl;//-1
	cout<<b.compare(c)<<endl;//-1

	cout<<c.compare(a)<<endl;//1
	cout<<c.compare(b)<<endl;//1

	cout<<a.compare(a)<<endl;//兩個字符串相同時返回0
	cout<<b.compare(b)<<endl;//0
	cout<<c.compare(c)<<endl;//0
	cout<<c.compare("ww11ok15011com")<<endl;//0
}

8、c_str() 

//C++ c_str()成員函數輸出字符串【C++ string|C++ 定義string變量並且初始化該變量|C++ 初始化string變量】

#include<iostream>
#include<string>
//#include<vector>
using namespace std;

void main()
{
	string a("hello,OK2002.com!");//定義string變量a並且初始化該變量
	cout<<a.c_str()<<endl;//輸出string變量a:hello,OK2002.com!

	string b;//定義string變量
	b="ok1500.com";//初始化string變量
	cout<<b.c_str()<<endl;//輸出string變量b:ok1500.com
}


9、C++ copy()| C++ memset()

//C++ 將一個字符串中的指定字符複製到數組中【C++ copy()| C++ memset()】

#include<iostream>
#include<string>
//#include<vector>
//#include<list>
using namespace std;

void main()
{
char a[30];
memset(a,'\0',30);
string str="1234567890";
str.copy(a,5);//在string變量str中複製5個字符到數組a中
cout<<a<<endl;
}


10、data()

//C++ 返回字符串的第一個字符【C++ data()】

#include<iostream>
#include<string>
//#include<vector>
//#include<list>
using namespace std;

void main()
{
	string a="ok2002.com";
	cout<<*a.data()<<endl;//*a.data()返回a字符串的第一個字符'o'

	string b="hello";
	cout<<*b.data()<<endl;//*b.data()返回b字符串的第一個字符'h'
}


11、拷貝構造函數

//C++ 拷貝構造函數,生成某一字串的複製品【C++ string】

#include<iostream>
#include<string>
//#include<vector>
//#include<list>
using namespace std;

void main()
{
	string a="ok2002.com";
	string b(a);//拷貝構造函數,生成a字串的複製品
	cout<<b<<endl;
	cout<<a<<endl;
}



12、創建string時生成N個相同字符的字符串|C++ 改變string變量a的值


//C++ 創建string時生成N個相同字符的字符串|C++ 改變string變量a的值【C++ string】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a(10,'A');
	cout<<a<<endl;
	//a(20,'B');//這種是不對的,必須在創建string時生成N個相同字符的字符串
	a="ABCD";//如果想改變string變量a的值,可以這樣做
	cout<<a<<endl;
}


13、string b(a.begin(),a.end());複製


#include<iostream>
#include<string>
using namespace std;

void main()
{
string a="www.ok2002.com";
string b(a.begin(),a.end());//以區間a.begin()和a.end()(不包含a.end())內的字符作爲字符串b的初值
cout<<b<<endl;
}



14、賦值

//C++ 字符串賦值的兩種方式|C++ 初始化字符串變量【C++ string】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a;//定義string變量a

	a="hello";//將a初始化爲 hello
	cout<<a<<endl;

	a.assign("ok2002.com");//把a賦值爲 ok2002.com
	cout<<a<<endl;
}



15、swap() 交換

//C++ 交換兩個字符串內容【C++ string | C++ swap()】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a="www.ok2002.com";
	string b="www.ok1500.com";

	cout<<"交換前:"<<endl;
	cout<<a<<endl;
	cout<<b<<endl;

	a.swap(b);//交換兩個字符串內容

	cout<<"交換後:"<<endl;
	cout<<a<<endl;
	cout<<b<<endl;
} 


16、初始化字符串(兩種方法) | C++ 創建string變量時賦值 |C++ string變量重新賦值【C++ string 】


//C++ 初始化字符串(兩種方法) | C++ 創建string變量時賦值 |C++ string變量重新賦值【C++ string 】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	
	string a="ok2002.com";
	cout<<a<<endl;//輸出:ok2002.com
	a="abc";//重新賦值
	cout<<a<<endl;//輸出:abc

	string b("ok1500.com");
	cout<<b<<endl;//輸出:ok1500.com
	b="hello";//重新賦值
	cout<<b<<endl;//輸出:hello

} 


17、C++ insert()

//C++ 字符串中插入字符串【C++ string |C++ insert()】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a;
	a.insert(0,"OK");//在0的位置插入字符串 OK
	a.insert(2,"2002");//在2的位置插入字符串 2002
	a.insert(6,".");//在6的位置插入字符串 .
	a.insert(7,"com");//在7的位置插入字符串 com
	cout<<a<<endl;//輸出本站網址:OK2002.com
} 


18、erase()


//C++ 從第一個字符開始保留指定個字符【C++ string |C++ erase()】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a="ABCD";
	a.erase(1);//從第一個字符開始保留1個字符
	cout<<a<<endl;//輸出:A

	a="ABCD";
	a.erase(2);//從第一個字符開始保留2個字符
	cout<<a<<endl;//輸出:AB

	a="ABCD";
	a.erase(3);//從第一個字符開始保留3個字符
	cout<<a<<endl;//輸出:ABC
}


19、max_size()

//C++ 字符的可能最大個數 【C++ string | C++ max_size()】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a="AA";
	cout<<a.max_size()<<endl;

	string b="www.ok2002.com";
	cout<<b.max_size()<<endl;
}

/*--------------------測試結果:
4294967293
4294967293
Press any key to continue
---------------------------*/
 



20、C++ capacity()

//C++ 重新分配之前的字符容量【C++ string | C++ capacity()】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a="AA";
	cout<<a.capacity()<<endl;//返回重新分配之前的字符容量

	string b="www.ok2002.com";
	cout<<b.capacity()<<endl;//返回重新分配之前的字符容量
}

/*--------------------測試結果:
31
31
Press any key to continue
---------------------------*/

21、C++ reserve() |C++ capacity()


//C++ 保留一定量內存以容納一定數量的字符【C++ string | C++ reserve() |C++ capacity()】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a="AA";

	cout<<a.capacity()<<endl;//31 返回重新分配之前的字符容量

	a.reserve(100);//保留一定量內存以容納一定數量的字符
	cout<<a.capacity()<<endl;//127

	a.reserve(20);
	cout<<a.capacity()<<endl;//127

	a.reserve(300);
	cout<<a.capacity()<<endl;//319
}


22、串聯字符串

//C++ 串聯字符串【C++ string】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string b;
	b=b+"AA"+"BB"+"CC";//串聯字符串
	cout<<b<<endl;//輸出:AABBCC

	string a="Hello,";
	a=a+"OK"+"2002"+".com!";//串聯字符串
	cout<<a<<endl;//輸出:Hello,OK2002.com!
}



23、C++ size()| C++ length() 

//C++ 字符數量 | C++ 字符個數【C++ string | C++ size()| C++ length() |本實驗中size()和length()表示出一至的特性】


#include<iostream>
#include<string>
using namespace std;


void main()
{
	string a="AA";
	string b="ABCD";
	cout<<a.size()<<" "<<b.size()<<endl;//2 4
	cout<<a.length()<<" "<<b.length()<<endl;//2 4
}


24、C++ at() |C++ [ ]


//C++ 存取單一字符|C++ 訪問一個字符(兩種訪問方式)【C++ string | C++ at() |C++ []】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a="ABCD";

	//第一種訪問方式(更安全,能檢查出不存在的元素並且發現錯誤時可以有效阻止錯誤程序的繼續執行)
	cout<<a.at(0)<<endl;//A
	cout<<a.at(1)<<endl;//B
	cout<<a.at(2)<<endl;//C
	cout<<a.at(3)<<endl;//D

	cout<<endl;

	//第二種訪問方式
	cout<<a[0]<<endl;//A
	cout<<a[1]<<endl;//B
	cout<<a[2]<<endl;//C
	cout<<a[3]<<endl;//D
}


25、getline()

//C++ 從鍵盤輸入中讀取一行字符串【C++ getline()】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	char b[100];
	cout<<"請輸入一行字符串:"<<endl;
	cin.getline(b,' '); 
	cout<<b<<endl;
}

26、C++ >> | C++ <<

//C++ 從stream讀取某值|C++ 將謀值寫入stream【C++ >> | C++ <<】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	char b[100];
	cout<<"請輸入一行字符串:"<<endl;
	cin.getline(b,' '); 
	cout<<b<<endl;

	//輸入string類型的變量值
	string a;
	cout<<"input a string:"<<endl;
	cin>>a;
	cout<<a<<endl;
	
	//輸入int類型的變量值
	int x;
	cout<<""<<endl;
	cin>>x;
	cout<<x<<endl;
}

27、substr()

//C++ 從某個字符開始的所有字符 | C++ 子字符串【C++ substr()】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string s("123456789ABC");
	string sub=s.substr(8);//返回從第8+1個元素開始的所有字符
	cout<<s<<endl;
	cout<<sub<<endl;
}



28、C++ data() | C++ c_str()


//C++ 將內容以字符數組形式返回|C++ 將內容以C_string返回【C++ data() | C++ c_str() | C++ string】

#include<iostream>
#include<string>
using namespace std;

void main()
{
	string a="ABC";
	cout<<a.data()<<endl;//將內容以字符數組形式返回,輸出:ABC
	cout<<a.c_str()<<endl;//將內容以C_string返回,輸出:ABC
}


29、C++ 類寫的模板向量按不同數據項排序 sort()

//C++ 類寫的模板向量按不同數據項排序 
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
using namespace std;

class DATA
{
public:
	int id;
	string name; 
};

class WAY
{
public:
	template<class T>
	void display(T&v)
	{
		typename T::iterator u;
		for(u=v.begin();u!=v.end();u++)
			cout<<u->id<<" "<<u->name<<endl;
		cout<<endl;
	}
};

bool greaterById(DATA &a,DATA &b)
{
	return a.id>b.id;
}

bool greaterByName(DATA &a,DATA &b)
{
	return a.name>b.name;
}

void main()
{
	int lastId=0;
	string name[]={"孔明","劉備","曹操","張飛","關羽","周俞","魯肅"};
	int len=sizeof(name)/sizeof(string);
	vector<DATA>v;
	WAY way;
	DATA p;
	for(int i=0;i<len;i++)
	{
		p.id=++lastId;
		p.name.assign(name[i]);
		v.push_back(p);
	}
	way.display(v);

	cout<<"按name逆序排序:"<<endl;
	sort(v.begin(),v.end(),greaterByName);
	way.display(v);

	cout<<"按id逆序排序:"<<endl;
	sort(v.begin(),v.end(),greaterById);
	way.display(v);
}


30、C++ 檢測越界功能的程序

//C++ 檢測越界功能的程序 | C++ 數組訪問方法增加檢查下標越界功能 | C++ 重載運算符[]【沒有越界時返回對應位置上的字符,越界時返回'#'】
#include<iostream.h>
#include<string.h>

class OP
{
	char *str;
	int len;
public:
	OP(char *a)
	{
		str=new char[strlen(a)+1];//在堆中申請內存
		strcpy(str,a);
		len=strlen(a);
	}
	~OP()
	{
		delete str;//析構函數中釋放內存空間
	}
	char operator[](int i)
	{
		if(i>=len)
			return '#';//越界時返回'#'
		else
			return *(str+i);//沒有越界時返回對應位置上的字符
	}
};

void main()
{
	OP str("www.ok2002.com");
	for(int i=0;i<20;i++)
		cout<<str[i];//沒有越界時返回對應位置上的字符,否則將返回'#'
	cout<<endl;
}










發佈了58 篇原創文章 · 獲贊 8 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章