ca33a_demo_c++_新旧代码的兼容char数组与vector_string相互转换
/*ca33a_demo_c++33_CppPrimer_新旧代码的兼容_txwtech
旧代码:数组和c风格字符串
新代码:vector和string
相互转换:
c风格字符串<- ->string
数组<- ->vector
C3867错误。
https://docs.microsoft.com/zh-cn/cpp/error-messages/compiler-errors-2/compiler-error-c3867?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-CN%26k%3Dk(C3867)%26rd%3Dtrue&view=vs-2019
const char *str=st.c_str(); c_str后面的括号不要忘记写上
*/
1 /*ca33a_demo_c++33_CppPrimer_新旧代码的兼容_txwtech 2 旧代码:数组和c风格字符串 3 新代码:vector和string 4 相互转换: 5 c风格字符串<- ->string 6 数组<- ->vector 7 8 C3867错误。 9 https://docs.microsoft.com/zh-cn/cpp/error-messages/compiler-errors-2/compiler-error-c3867?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-CN%26k%3Dk(C3867)%26rd%3Dtrue&view=vs-2019 10 const char *str=st.c_str(); c_str后面的括号不要忘记写上 11 12 */ 13 14 #include <iostream> 15 #include <string> 16 #include <vector> 17 using namespace std; 18 19 int main() 20 { 21 string st("Hello World"); 22 st = st + " bill"; 23 cout <<"c++:string st: "<< st << endl; 24 25 const char *str=st.c_str();//c++转c风格字符串,必须加上const,常字符指针 26 cout << "c风格*str: "<<str << endl; 27 //getchar(); 28 const size_t arr_size = 6; 29 int int_arr[arr_size] = {0,1,2,3,4,5}; 30 vector<int> ivec(int_arr, int_arr + arr_size);//数组转vector, int_arr默认指针指向第一个,int_arr + arr_size指向最后一个的后面 [ 开区间,保活。 )闭区间不包括。前包后不包 31 cout << "间隔ivec:" << endl; 32 for (vector<int>::iterator itr = ivec.begin(); itr != ivec.end(); ++itr) 33 { 34 cout << *itr << endl; 35 } 36 cout << "间隔ivec2:" << endl; 37 vector<int> ivec2(int_arr+1,int_arr+4);//2,3,4的数据赋值到vector 38 for (vector<int>::iterator itr = ivec2.begin(); itr != ivec2.end(); ++itr) 39 { 40 cout << *itr << endl; 41 } 42 43 return 0; 44 }
1 /*ca33b_demo_c++_txwtech 2 vector数据放入数组 3 */ 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 8 using namespace std; 9 10 int main() 11 { 12 vector<int> ivec; 13 int ival; 14 15 cout << "Enter number:(Ctrl+Z to end)" << endl; 16 while (cin >> ival) 17 ivec.push_back(ival); 18 19 int *parr = new int[ivec.size()];//创建动态数组 20 size_t ix = 0; 21 for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter, ++ix) 22 parr[ix] = *iter;//vector数据放入数组 23 for (ix = 0; ix < ivec.size(); ++ix) 24 { 25 cout <<"vector to 数组" <<*(parr + ix) << endl; 26 } 27 delete[] parr; 28 return 0; 29 }
/*习题4.34—4.35—c++_txwtech
1.读入一组string数据,放入vector
3.vector字符串->字符指针数组,vector每个字符串--创建新的动态字符数组,
4.vector字符串复制到字符数组
5.字符数组放到动态字符指针数组里面
//cin与getline方法,cin遇到空格就默认一个字符串,getline不识别空格,直到回车,默认一个字符串输入结束
while (getline(cin, str))
svec.push_back(str);
*/
1 /*习题4.34—4.35—c++_txwtech 2 1.读入一组string数据,放入vector 3 3.vector字符串->字符指针数组,vector每个字符串--创建新的动态字符数组, 4 4.vector字符串复制到字符数组 5 5.字符数组放到动态字符指针数组里面 6 //cin与getline方法,cin遇到空格就默认一个字符串,getline不识别空格,直到回车,默认一个字符串输入结束 7 */ 8 #define _CRT_SECURE_NO_WARNINGS 9 #include <iostream> 10 #include <string> 11 #include <vector> 12 13 using namespace std; 14 15 int main() 16 { 17 vector<string> svec; 18 string str; 19 cout << "enter strings: (ctrl+Z) to end" << endl; 20 //while (cin >> str) //cin与getline方法,cin遇到空格就默认一个字符串,getline不识别空格,直到回车,默认一个字符串输入结束 21 while (getline(cin, str)) 22 svec.push_back(str);//1 23 //创建动态字符指针数组 24 //数组里面放的指针 25 char **parr = new char*[svec.size()]; 26 27 size_t ix= 0; 28 for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter, ++ix) 29 { 30 char *p = new char[(*iter).size() + 1];//*iter指向的是一个字符串//3 31 strcpy(p,(*iter).c_str());//vector转c风格//4 32 parr[ix] = p;//动态字符数组赋值给字符指针//5 33 } 34 cout << "content of vector: " << endl; 35 for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter) 36 { 37 cout << *iter << endl; 38 } 39 cout << "Content of charactor arrays: " << endl; 40 for (ix = 0; ix != svec.size(); ++ix) 41 { 42 cout << parr[ix] << endl; 43 } 44 //每个指针delete 45 for (ix = 0; ix != svec.size(); ++ix) 46 { 47 delete parr[ix]; 48 } 49 delete[] parr; 50 51 //getchar(); 52 return 0; 53 }
欢迎讨论,相互学习。
cdtxw@foxmail.com