4.1

本文通过多个实例展示了如何使用C++中的vector容器进行数据读取与复制操作,包括从标准输入读取整数并复制到动态数组,以及如何处理C风格字符串和string对象的连接操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
*编写程序由从标准输入的设备读入的元素数据建立一个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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值