4.1

来源:互联网 发布:top域名多少钱 编辑:程序博客网 时间:2024/05/21 21:47

/*
 *编写程序由从标准输入的设备读入的元素数据建立一个int型vector对象,
 *然后动态创建一个与该vector对象大小一致的数组,
 *把vector对象的所有元素复制给新数组。
 */
#include <iostream>
#include <vector>

using namespace std;

int main()
{
 vector<int> ivec;
 int k,*p, *q, *d;
 cout << "输入整数" << endl;
 while(cin >> k)
 {
  ivec.push_back(k);
 }
 
 p = new int[ivec.size()];
 q=p;
 d=p;

 for(vector<int>::iterator iter = ivec.begin();iter != ivec.end();++iter,++q)
 {
  *q = *iter;
  
 }
 for(;d != p+ivec.size();++d)
 {
  cout << *d;
 }

 delete [] p;

 system("pause");
 return 0;
}

 

 

 

 

 


/*
 *编写程序连接两个C风格字符串字面值,把结果存储在一个C风格字符串中。
 *然后再编写程序连接两个string类型字符串,这两个string类型的字符串
 *与前面的C风格字符串字面值具有相同的内容。
 */


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

int main()
{
 const char *str1 = "who and who ";
 const char *str2 = "will get together !";
 char *str3;
 string str5, str6, str7;

 str3 = new char[strlen(str1) +strlen(str2) +1];
 strcpy(str3,str1);
 strcat(str3,str2);
 for(int i = 0; i != (strlen(str1) +strlen(str2) +1); ++i)
  cout << *(str3 + i);

 cout << endl;

 str5 = "who and who ";
 str6 =  "will get together !";
 str7 = str5 + str6;

 cout << str7;


 delete [] str3;

 system("pause");
 return 0;
}

 

 

 

 

 

 

 

 

 

 

 


/*
 *编写程序连接两个C风格字符串字面值,把结果存储在一个C风格字符串中。
 *然后再编写程序连接两个string类型字符串,这两个string类型的字符串
 *与前面的C风格字符串字面值具有相同的内容。
 */


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

int main()
{
 char *str1;

 string str3;


 cout << "输入字符";
 cin >> str3;

 str1 = new char[ strlen(str3.c_str())+1 ];
 
 strcpy(str1,str3.c_str());

 for(char *str2 = str1; str2 != str1 + strlen(str3.c_str() ) + 1 ; ++str2 )
  cout << *str2;
 
 delete [] str1;
 system("pause");
 return 0;
}

 

 

 

 

 

 

 

 


*
 *编写程序用int类型数组初始化vector对象;
 *再把int型vector复制给int型数组;
 */


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

int main()
{
 const size_t arr_size = 6;
 int int_arr[arr_size] = {0 ,1, 2, 3, 4, 5};
 int *ip,*iq;

 vector<int> ivec(int_arr, int_arr +arr_size);

 for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
 {
  cout << *iter << ' ';
 }

 cout << endl;

 ip = new int[ivec.size()];
 iq = ip;

 for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter,++iq)
 {
  *iq = *iter;
  
 }
 

 for(int i = 0; i != 6; ++i)
 {
  cout << ip[i] << endl;
 }

 delete [] ip;

 system("pause");
 return 0;
}

 

 

 

 

 

 

 


/*
 *编写程序读入一组string类型的数据,并将它们存储在vector中。接着,把该vector对象复制给一个字符指针数组。
 *为vector中的每个元素创建一个新的字符数组,并把该vector元素的数据复制到相应的字符数组中,
 *最后把指向该数组的指针插入字符指针数组。数组数组内容。
 */


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

int main()
{
 vector<string> strvec;
 string str;

 cout<< "输入字符串";

 while(cin >> str)
 {
  strvec.push_back(str);
 }

 char **p = new char*[strvec.size()];
 size_t ix = 0;

 for (vector<string>::iterator iter = strvec.begin(); iter != strvec.end(); ++iter,++ix)
 {
  char *q = new char[(*iter).size() +1];
  strcpy(q, (*iter).c_str()); //注意将string类字符转成C风格字符;
  p[ix] = q;
 }

 for(int i = 0; i != strvec.size(); ++i)
 {
  for(int j = 0; j != strlen(p[i]) +1; ++j)
  {
   cout << (p[i][j]);
  }
  cout << endl;
 }

 for(int i = 0; i != strvec.size(); ++i)
 {
  delete [] p[i];
 }

 delete [] p;
 

 

 system("pause");
 return 0;
}