C++ String内置函数

tech2022-11-25  85

子串截取:substr(start,size)

String转字符串:c_str()

查找子串:find()

判断是否为空:empty()

替换:replace(start,size,str2)

倒置:reverse(str.begin(),str.end())

长度:length()

插入:insert(start,str,size)

//#include<iostream> //#include<string> //#include<vector> //using namespace std; //int main() //{ // //函数在p0位置插入字符串s中pos开始的前n个字符 // //string &insert(int p0, const char *s); // string str = "abcdefghijklmn"; // str.insert(1, "@#$%"); // cout << str << endl; //a@#$%bcdefghijklmn // // //string &insert(int p0, const char *s, int n); // str = "abcdefghijklmn"; // str.insert(1, "@#$%",2); // cout << str << endl;//a@#bcdefghijklmn // // //string &insert(int p0, const string &s, int pos, int n); // str = "abcdefghijklmn"; // str.insert(1, "@#$%", 2,3);//包前包后 // cout << str << endl;//a$%bcdefghijklmn // // system("pause"); // return 0; //} //#include<iostream> //#include<iomanip> //using namespace std; //int d[9][9];//d数组 存放每个顶点到每个顶点的最短路径长度 //int p[9][9];//p数组 存放最短路径 //int main() { // //初始化数组 // for (int i = 0; i <5; i++) // for (int j = 0; j < 5; j++) { // if (i == j) // d[i][j] = 0; // else // d[i][j] = 0xfffffff; // p[i][j] = j+1; // } // //向邻接矩阵中写入权值 // { // d[0][1] = 50; // d[1][4] = 80; // d[2][1] = 30; d[2][3] = 20; // d[3][4] = 70; // d[4][0] = 65; d[4][2] = 100; // } // //更新存放最短距离的d邻接矩阵 更新存放路径的p数组 // for (int k = 0; k < 5; k++) // { // for (int i = 0; i < 5; i++) // for (int j = 0; j < 5; j++) { // if (d[i][j] > d[i][k] + d[k][j]) { // d[i][j] = d[i][k] + d[k][j]; // p[i][j] = p[i][k]; // } // } // cout << "当k="<<k+1<<"时候" << endl; // for (int i = 0; i < 5; i++) // { // for (int j = 0; j < 5; j++) // { // if (d[i][k]+d[k][j] >= 0xfffffff) // cout << setw(4) << "**"; // else // cout << setw(4) << d[i][k]+d[k][j]; // } // cout << endl; // } // cout << "B"<<k+1<<endl; // for (int i = 0; i < 5; i++) // { // for (int j = 0; j < 5; j++) // { // if (d[i][j] == 0xfffffff) // cout << setw(4) << "**"; // else // cout << setw(4) << d[i][j]; // } // cout << endl; // } // cout << endl; // } // cout << "R数组" << endl; // for (int i = 0; i < 5; i++) // { // for (int j = 0; j < 5; j++) // { // cout << setw(4) << p[i][j]; // } // cout << endl; // } // system("pause"); // return 0; //} //#include<iostream> //#include<cstdlib> //#include<algorithm> //#include<string> //using namespace std; //int ans = 0; //int main() { // string s = "123456789"; // /**下面为next_permutation的用法*/ // do // { // for (int i = 1; i <= 7; i++) { // string a = s.substr(0, i); // int inta = atoi(a.c_str()); // if (inta > 100) // break; // for (int j = 1; j <= 9-i-1; j++) { // string b = s.substr(i,j); // string c = s.substr(i+j); // int intb = atoi(b.c_str()); // int intc = atoi(c.c_str()); // if (intb%intc==0&&inta + intb / intc==100) { // cout << inta << ' ' << intb << ' ' << intc << endl; // ans++; // } // } // // } // } while (next_permutation(s.begin(), s.end())); // cout << ans << endl; // /**下面为prev_permutation的用法*/ // // /*do // { // printf("%d %d %d\n",s[0],s[1],s[2]); // } // while(prev_permutation(s,s+3));*/ // system("pause"); // return 0; //} //#include<iostream> //#include<algorithm> //using namespace std; //int main() { // for (int i = 0; i < 50; i++){ // for (int j = 0; j < 60; j++) { // if (2.3 * i + 1.9 * j == 82.3) // cout << i << ' '<<j << endl; // } // } // system("pause"); // return 0; //} #include<iostream> #include<stack> #include<queue> #include<iomanip> #include<cstdlib> #include<string> #include<map> #include<algorithm> using namespace std; bool cmp(int a, int b) { if (a < b)return false; else return true; } int main() { int array[4] = { 1,3,5,2 }; //排序 sort(array, array + 4,cmp); //格式化输出 for (int i = 0; i < 4; i++) { cout << setw(4) << array[i]; } cout << endl; //全排列 do { for (int i = 0; i < 4; i++) { cout << setw(4) << array[i]; } cout << endl; } while (prev_permutation(array, array + 4)); //string转为字符串 字符串转为int string str = "12930"; int strint = atoi(str.c_str()); cout << strint + 1 << endl; //int转化为string类型 int numb = 123456; string str_1 = to_string(numb); string str_2 = "abcdefgh"; //截取字符串 string str_3 = str_2.substr(1, 3); cout << "截取字符串为:" << str_3 << endl; //查找子串 cout << "c的位置为" << str_2.find("c")<<endl; cout << "cb的位置为" << (str_2.find("cb")!=str_2.npos) << endl; //替换 str_2.replace(2, 4, "*"); cout << "替换后的字符串为:" << str_2 << endl; //倒置 reverse(str_2.begin(),str_2.end()); cout << "倒置后的字符串为" << str_2 << endl; //判断是否为空 //保留两位小数 cout << fixed << setprecision(2) << 3.122 << endl; //结束格式输出 cout.unsetf(ios::fixed); //保留两位有效数字 cout << setprecision(2) << 3.1222 << endl; //队列使用 queue<int> que; //入队操作 for (int i = 0; i < 10; i++){ que.push(i); } //头节点尾节点 cout << que.front() << ' ' << que.back() << endl; //出队操作 que.pop(); cout << que.front()<<' '<<que.back()<<endl; //栈使用 stack<int> sta; //入栈操作 for (int i = 0; i < 10; i++) { sta.push(i); } //栈顶节点 cout << sta.top() << endl; //出栈操作 sta.pop(); cout << sta.top() << endl; map<string, int> mp; for (int i = 0; i < 10; i++) { mp["abc"]++; } cout << "map:"<<mp["abc"] << endl; system("pause"); return 0; } //#include<iostream> //#include<string> //int ans=0; //using namespace std; //int main() { // for(int i=10000;i<=99999;i++){ // string a = to_string(i); // if (a.find('4') == -1) ans++; // } // cout << ans << endl; // system("pause"); // return 0; //} #include<algorithm> using namespace std; int main(){ /*char a[10],b[10],c[10]; scanf("%s %s %s",a,b,&c); cout<<a<<' '<<b<<' '<<c<<endl; cout<<strlen(a)<<' '<<b<<' '<<c<<endl; float e=9.98; printf("%.1f",de); system("pause"); return 0;*/ char ch[3][10]; for(int i=0;i<3;i++){ cin>>ch[i]; } for(int i=0;i<3;i++){ cout<<ch[i]<<' '; } string str=ch[1]; cout<<str<<endl; system("pause"); return 0; }
最新回复(0)