c++学习-字符串
字符数组和 string类型比较的区别
#include<iostream>
#include<string>
using namespace std;
class area{
public:
area(){ cout << "gouzao:" <<endl; }
area(int i){ this->i = i; cout << "gouzao:" << i << endl; }
area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; }
~area(){ cout << "xigou "<<i << endl; }
int get()
{
return w*h;
}
void set(int w, int h)
{
this->w = w;
this->h = h;
}
private:
int w;
int h;
int i;
};
int main()
{
//char 数组做比较需要 strcmp
//char a[] = "ab";
//char b[] = "ab";
//cout << (a==b) << endl;//不相等,比较的是两个地址
//cout << strcmp(a, b) << endl;
//cout << strcmp(a, "ab") << endl;
string a = "ab";
cout << (a == "ab") << endl; //wright
return 0;
}
赋值比较:
int main()
{
char ch1[10] = "ab";
char ch2[10] = "cd";
//ch1 = ch2; //错误
strcpy(ch1, ch2);
string ch3 = "ab";
string ch4 = "cd";
ch3 = ch4;
cout << ch3 << endl;
cout << ch4 << endl;
return 0;
}
字符串赋值:
#include<iostream> #include<string> using namespace std; class area{ public: area(){ cout << "gouzao:" <<endl; } area(int i){ this->i = i; cout << "gouzao:" << i << endl; } area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; } ~area(){ cout << "xigou "<<i << endl; } int get() { return w*h; } void set(int w, int h) { this->w = w; this->h = h; } private: int w; int h; int i; }; int main() { //char ch1[10] = "ab"; //char ch2[10] = "cd"; ////ch1 = ch2; //错误 //strcpy(ch1, ch2); string ch3 = "ab"; string ch4 = "cd"; ch3 = ch4; ch3.assign(ch4,0,1); //部分元素赋值给ch3 cout << ch3 << endl; cout << ch4 << endl; return 0; }
字符串合并:
int main() { string ch1 = "ab"; string ch2 = "cd"; ch1 = ch1 + ch2; cout << ch1 << endl; cout << ch2 << endl; return 0; }
计算长度:
int main() { string ch1 = "ab"; string ch2 = "cd"; ch1 = ch1 + ch2; cout << ch1 << endl; cout << ch2 << endl; //计算字符长度 cout << strlen(ch1.c_str()) << endl; cout << ch1.size() << endl; cout << ch1.length() << endl; return 0; }
字符串部分合并:
int main() { //char ch3[10] = "ab"; //char ch4[10] = "abcdefg"; //strncat(ch3, ch4, 3); //cout << ch3 << endl; string ch1 = "ab"; string ch2 = "cdefg"; ch1.append(ch2, 2,2); cout << ch1 << endl; return 0; }
字符串替换:
int main() { //char ch3[10] = "ab"; //char ch4[10] = "abcdefg"; //strncpy(ch3, ch4, 3); //cout << ch3 << endl; string ch1 = "ab"; string ch2 = "cdefg"; char ch5[] = "cdefg"; char ch6 = 'A'; //ch1.replace(0,1,ch2, 2,2); //ch1.replace(0, 1, ch5, 2, 2);//支持char型数组 //ch1.replace(0, 1, 2, ch6);//支持char 字符 cout << ch1 << endl; return 0; }
字符串拷贝:
int main() { char ch3[10] = "abffffff"; char ch4[10] = "abcdefg"; memmove(ch3, ch4, 4); cout << ch3 << endl; string ch1 = "abcdefghkjklmn"; char ch5[] = "cdefg"; cout << ch5 << endl; ch1.copy(ch5,2,3); cout << ch5 << endl; return 0; }
字符串插入:
int main() { string str1 = "abcdefg"; string str2 = "abc"; str1.insert(2,str2, 0,2); cout << str1 << endl; return 0; }
字符串删除:
int main() { string str1 = "abcdefg"; str1.erase(2,2); cout << str1 << endl; return 0; }
删除字符串:
int main() { string str1 = "abcdefg"; str1.erase(2,2); cout << str1 << endl; str1.erase(2);//第二个以后全部删除 cout << str1 << endl; str1.erase();//清空一个字符串 cout << str1 << endl; return 0; }
字符串查找:
int main() { char ch1[15] = "hello world!"; char *p, c = '4'; p = strchr(ch1, c);//返回找到的w字符的地址,找不到则返回空指针 if (p) { cout << p << endl; cout << p - ch1 << endl; //计算找到的字符的下标 } return 0; }
int main() { string str1("abcdefg"); int f = str1.find('b', 1); //从第一个字符开始查找 if (f == string::npos) { cout << "not find " << endl; } cout << f << endl; f = str1.find_first_not_of('b', 1); //查找第一个不是b的字符 cout << f << endl; f = str1.find_last_of('b'); //查找最后一个b的位置 cout << (int)string::npos << endl; return 0; }
判断字符串是否为空:
int main() { string s1 = ""; if (s1.empty()) { cout << "empty" << endl; }else{ cout << "not empty" << endl; } return 0; }
字符串交换:
int main() { char ch1[15] = "ofru"; char ch2[15] = ""; swap(ch1,ch2); cout << ch1 << endl; cout << ch2 << endl; string str1 = "ab"; string str2 = ""; str1.swap(str2); cout << str1 << endl; cout << str2 << endl; return 0; }
string字符串转char型
int main() { string str1 = "abcde"; const char *p; p = str1.c_str(); cout << p << endl; return 0; }
字符串传参:
int get_length(const char *p) //int get_length(const char p[]) { int count = 0; while (*p) { count++; p++; } return count; } int main() { char a[] = "abc"; char *p = "defg"; cout << get_length(a) << endl; cout << get_length(p) << endl; return 0; }
字符串函数返回:
char *get(const char *str) { char *p = new char[strlen(p) +1]; strcpy(p, str); return p; } int main() { char a[] = "abc"; char *p = get(a); cout << p << endl; char *p2 = get("abc"); cout << p << endl; char *p3 = "abc"; char *p4 = get(p3); cout << p4 << endl; delete[]p; return 0; }
结构体:
#include<iostream> #include<string> using namespace std; struct man{ public: int age; char *name; }; int main() { struct man one={ 12, "中国" }; cout << one.age << endl; cout <<one.name<< endl; return 0; }
结构体与构造函数:
#include<iostream> #include<string> using namespace std; struct man{ man(int c_age); //public: int age; char *name; string name1; }; man::man(int c_age) { age = c_age; } int main() { man one(4); cout << one.age << endl; return 0; }
结构体赋值:
#include<iostream> #include<string> using namespace std; struct man{ int age; char *name; string name1; }; int main() { man one = { 1, "one", "one1" }; man two = { 1, "two", "two1" }; one = two; cout << one.age << endl; cout << one.name << endl; cout << one.name1 << endl; return 0; }
结构体与函数:
#include<iostream> #include<string> using namespace std; struct time{ int hour; int minute; }; const int perhour = 60; time sum(time t1, time t2); int main() { time t1 = {23,4}; time t2 = { 5, 14 }; time total = sum(t1,t2); cout << total.hour << endl; cout << total.minute << endl; return 0; } time sum(time t1, time t2) { time total; total.minute = (t1.minute + t2.minute) % perhour; total.hour = t1.hour + t2.hour + ((t1.minute + t2.minute) / perhour); return total; }
指针方式返回:
#include<iostream> #include<string> using namespace std; struct time{ int hour; int minute; }; const int perhour = 60; time *sum(const time &t1, const time &t2); int main() { time t1 = {23,4}; time t2 = { 5, 14 }; time *total = sum(t1,t2); cout << total->hour << endl; cout << total->minute << endl; return 0; } time *sum(const time &t1, const time &t2) { time *total = new time; total->minute = (t1.minute + t2.minute) % perhour; total->hour = t1.hour + t2.hour + ((t1.minute + t2.minute) / perhour);
delete total; return total; }
引用方式传递返回:
#include<iostream> #include<string> using namespace std; const string &show(const string &str); int main() { string str1 = "abc"; string str2 = show(str1); cout << str2 << endl; return 0; } const string &show(const string &str) { return str; }