string
string的初始化,在C++中字符串是一種數據類型;
1:string的初始化,遍歷,字符串連接。
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main(void){
//string的初始化,在C++中字符串是一種數據類型;
string s1 = "abcdefghijk";
string s2("abcdefghijk");
string s3(s2);
string s4 = s1; ?//調用拷貝構造函數;
string s5(10, 'a');//10個空間中的字符都是'a';
s5 = s1;
cout<<"s3:"<<s3<<endl;
cout<<"s5:"<<s5<<endl;
//string的遍歷,重載了[]操作符;
//1、數組方式遍歷[]
for(int i = 0; i < s1.length(); i++){
cout<<s1[i]<<" "; ?//出現錯誤(下標越界),不會向外面剖出異常,引起程序的中斷;
}
cout<<endl;
//2、迭代器
string::iterator it;
for(it = s1.begin(); it != s1.end(); it++){
cout<<*it<<" ";
}
cout<<endl;
//3、函數at()遍歷
for(int i = 0; i < s1.length(); i++){
cout<<s1.at(i)<<" "; //會剖出異常,合理的解決下標越界;
}
cout<<endl;
//字符指針和string的轉換
//此時,把s1====>char * 把內存首地址給露出來;
printf("s1:%s \n", s1.c_str());
//s1中的內容拷貝到buf中;
char buf[123] = {0};
s1.copy(buf, 2, 0);//n, pos;下標從0開始拷貝2個字符到buf中,不會是C風格的,注意自己加上0結束標志;
cout<<buf<<endl;
//string子符串的連接
s1 = s1 + s2; //直接+就表:字符串的連接;
s1 += s2; //+=也是字符串的連接;
s1.append(s4); //調用方法append()也是字符串的連接;
cout<<s1<<endl;
return 0;
}
運行結果
?2:string的查找,替換
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main(void){
//字符串的查找和替換
? ? string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
? ? //1、第一次出現wbm的下標
? ? int index = s1.find("wbm", 0);?
? ? cout<<"index :"<<index<<endl;
? ? //2、求wbm每一次出現的數組下標
/*? int offindex = s1.find("wbm", 0);
? ? while(offindex != -1){
? ? ? ? cout<<"offindex :"<<offindex<<endl;
? ? ? ? offindex += strlen("wbm");
? ? ? ? offindex = s1.find("wbm", offindex);
? ? }*/
? ? //3、把小寫wbm換成大寫
? ? int offindex = s1.find("wbm", 0);?
? ? while(offindex != -1){
? ? ? ? cout<<"offindex :"<<offindex<<endl;
? ? ? ? s1.replace(offindex, strlen("wbm"), "WBM"); //從下標offindex開始,刪除n個字符,替換為后面的字符;
? ? ? ? offindex += strlen("wbm");
? ? ? ? offindex = s1.find("wbm", offindex);
? ? }
? ? cout<<"s1:"<<s1<<endl;
? ? string s3 = "aaa bbb ccc";
? ? s3.replace(0, 3, "AAA");? //替換的函數;
? ? cout<<"s3:"<<s3<<endl;
? ? return 0;
}
運行結果:
3:區間的刪除和插入
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(void){
//區間刪除和插入
? ? string s1 = "hello1 hello2 hell03";
? ? string::iterator it = find(s1.begin(), s1.end(), 'l');
? ? if(it != s1.end()){
? ? ? ? s1.erase(it); //刪除算法;
? ? }? ?
? ? cout<<"s1 :"<<s1<<endl;
? ? s1.erase(s1.begin(), s1.end()); //刪除從pos開始的n個字符;
? ? cout<<"s1全部刪除:"<<s1<<endl;
? ? cout<<"s1的長度:"<<s1.length()<<endl;
? ? string s2 = "BBB";
? ? s2.insert(0, "AAA");? //頭插法
? ? s2.insert(s2.length(), "CCC");//尾插法
? ? cout<<s2<<endl;
? ? return 0;
}
運行結果:
4:string的大小寫轉換-->函數指針
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(void){
? ? string s1 = "AAAbbb";
? ? transform(s1.begin(), s1.end(), s1.begin(), 0, toupper);//toupper可以是:函數的入口地址,函數對象,
? ? cout<<s1<<endl;
? ? string s2 = "AAAbbb";
? ? transform(s2.begin(), s2.end(), s2.begin(), 0, tolower);
? ? cout<<s2<<endl;
? ? return 0;
}
array和string的運算符重載
string類
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class MyString{
? ? public:
? ? ? ? friend ostream& operator<<(ostream &out, const MyString &s1);
? ? ? ? friend istream& operator>>(istream &in, MyString &s2);
? ? ? ? MyString(int len = 0){ //默認參數看我們是否自己開辟大小的空間;
? ? ? ? ? ? if(len != 0){?
? ? ? ? ? ? ? ? m_len = len;
? ? ? ? ? ? ? ? m_p = new char[m_len+1];
? ? ? ? ? ? ? ? memset(m_p, 0, m_len);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? m_len = 0;
? ? ? ? ? ? ? ? m_p = new char[m_len+1];? ??
? ? ? ? ? ? ? ? strcpy(m_p, "");
? ? ? ? ? ? }? ?
? ? ? ? }? ?
? ? ? ? MyString(const char *p){
? ? ? ? ? ? if(p == NULL){
? ? ? ? ? ? ? ? m_len = 0;
? ? ? ? ? ? ? ? m_p = new char[m_len+1];? ??
? ? ? ? ? ? ? ? strcpy(m_p, "");
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? m_len = strlen(p);
? ? ? ? ? ? ? ? m_p = new char[m_len+1];
? ? ? ? ? ? ? ? strcpy(m_p, p);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? MyString(const MyString &s){
? ? ? ? ? ? m_len = s.m_len;
? ? ? ? ? ? m_p = new char[m_len+1];
? ? ? ? ? ? strcpy(m_p, s.m_p);
? ? ? ? }
? ? ? ? MyString& operator=(const MyString &t){
? ? ? ? ? ? if(m_p){
? ? ? ? ? ? ? ? delete []m_p;
? ? ? ? ? ? ? ? m_p = NULL;
? ? ? ? ? ? ? ? m_len = 0;
? ? ? ? ? ? }
? ? ? ? ? ? m_len = t.m_len;
? ? ? ? ? ? m_p = new char[m_len+1];
? ? ? ? ? ? strcpy(m_p, t.m_p);
? ? ? ? ? ? return *this;
? ? ? ? }
? ? ? ? ~MyString(){
? ? ? ? ? ? if(m_p) {
? ? ? ? ? ? ? ? delete []m_p;? ? ? ? ?
? ? ? ? ? ? ? ? m_p = NULL;
? ? ? ? ? ? ? ? m_len = 0;
? ? ? ? ? ? }
? ? ? ? }
? ? public:
? ? ? ? MyString operator=(const char *p){
? ? ? ? ? ? if(m_p){
? ? ? ? ? ? ? ? delete []m_p;
? ? ? ? ? ? ? ? m_p = NULL;
? ? ? ? ? ? ? ? m_len = 0;
? ? ? ? ? ? }
? ? ? ? ? ? if(p == NULL){
? ? ? ? ? ? ? ? m_len = 0;
? ? ? ? ? ? ? ? m_p = new char[m_len+1];
? ? ? ? ? ? ? ? strcpy(m_p, "");
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? m_len = strlen(p);
? ? ? ? ? ? ? ? m_p = new char[m_len+1];
? ? ? ? ? ? ? ? strcpy(m_p, p);
? ? ? ? ? ? }
? ? ? ? ? ? return *this;
? ? ? ? }
? ? ? ? char& operator[](int index){
? ? ? ? ? ? return m_p[index];
? ? ? ? }? ? ?
? ? ? ? bool operator==(const char *p)const{? //判斷與字符串是否相等,看長度和里面的內容是否相等!!!
? ? ? ? ? ? if(p == NULL){
? ? ? ? ? ? ? ? if(m_len == 0){
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? if(m_len == strlen(p)){
? ? ? ? ? ? ? ? ? ? return !strcmp(m_p, p);
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? bool operator==(const MyString &s)const{
? ? ? ? ? ? if(m_len != s.m_len){
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? return !strcmp(m_p, s.m_p);
? ? ? ? }
? ? ? ? bool operator!=(const char *p)const{
? ? ? ? ? ? return !(*this == p);
? ? ? ? }? ?
? ? ? ? bool operator!=(const MyString &s)const{
? ? ? ? ? ? return !(*this == s);
? ? ? ? }
? ? ? ? int operator<(const char *p)const{
? ? ? ? ? ? return strcmp(m_p, p);
? ? ? ? }
? ? ? ? int operator<(const MyString &s)const{
? ? ? ? ? ? return strcmp(m_p, s.m_p);
? ? ? ? }
? ? ? ? int operator>(const char *p)const{
? ? ? ? ? ? return strcmp(p, m_p);
? ? ? ? }
? ? ? ? int operator>(const MyString &s)const{
? ? ? ? ? ? return strcmp(s.m_p, m_p);
? ? ? ? }
? ? ? ? //怎么樣把類的指針露出來.
? ? public:
? ? ? ? char *c_str(){? ? ?
? ? ? ? ? ? return m_p;
? ? ? ? }
? ? ? ? const char *c_str2(){
? ? ? ? ? ? return m_p;
? ? ? ? }
? ? ? ? int length(){
? ? ? ? ? ? return m_len;
? ? ? ? }
? ? private:
? ? ? ? int m_len;
? ? ? ? char *m_p;
};
ostream& operator<<(ostream &out, const MyString &s1){
? ? out<<s1.m_p;
? ? return out;
}
istream& operator>>(istream &in, MyString &s2){
? ? in>>s2.m_p;
? ? return in;
}
int main(void){? ??
? ? MyString s1;
? ? MyString s2("s2");
? ? MyString s3 = s2;
? ? MyString s4 = "s444444444444";
? ? s4 = "s22222222222";
? ? s4 = s2;
? ? s4[1] = '3';
? ? printf("%c\n", s4[1]); //測試[]改變值了嗎?
? ? cout<<s4<<endl;
? ? if(s2 == "s2"){
? ? ? ? cout<<"相等"<<endl;
? ? }else{
? ? ? ? cout<<"不相等"<<endl;
? ? }
? ? s3 = "aaa";
? ? int flag = (s3 < "bbb");
? ? if(flag < 0){
? ? ? ? cout<<"s3小于bbb"<<endl;??
? ? }else{
? ? ? ? cout<<"s3大于bbb"<<endl;
? ? }
? ? s3 = "adasf";
? ? strcpy(s3.c_str(), "sga");
? ? cout<<s3<<endl;
? ? MyString s9(100);//默認輸入要開辟字符串的空間大小;
? ? cout<<"請輸入一個數字 :";
? ? cin>>s9;
? ? cout<<s9<<endl;
? ? return 0;
}
運行結果
array類
#include<iostream>
using namespace std;
class Array{
? ? public:
? ? ? ? Array(int count);
? ? ? ? Array(const Array &t);
? ? ? ? ~Array();
? ? public:
? ? ? ? void setData(int i, int data);
? ? ? ? int getData(int i);
? ? ? ? int length();
? ? private:
? ? ? ? int len;
? ? ? ? int *p;
};
Array::Array(int count){
? ? len = count;
? ? p = new int[len];
}
//有指針,的進行深拷貝;
Array::Array(const Array &t){
? ? len = t.len;
? ? p = new int[len];
? ? for(int i = 0; i < t.len; i++){
? ? ? ? p[i] = t.p[i];
? ? }
}
Array::~Array(){
? ? if(p){
? ? ? ? delete []p;
? ? ? ? p = NULL;
? ? }
}
void Array::setData(int i, int data){
? ? p[i] = data;
}
int Array::getData(int i){
? ? return p[i];
}
int Array::length(){
? ? return len;
}
int main(void){
? ? Array array(10);
? ? int i;
? ? for(i = 0; i < array.length(); i++){? ? ?
? ? ? ? array.setData(i, i);
? ? }
? ? for(i = 0; i < array.length(); i++){
? ? ? ? cout<<array.getData(i)<<" ";
? ? }
? ? cout<<endl;
? ? Array array1 = array;
? ? for(i = 0; i < array1.length(); i++){
? ? ? ? cout<<array1.getData(i)<<" ";
? ? }
? ? cout<<endl;
? ? return 0;
}
運行結果