习题4.7
编写必要的代码将一个数组赋给另一个数组,然后把这段代码改用Vector实现,考虑如何把一个Vector赋给另一个Vector.
#include<iostream>
#include<vector>
intmain()
{
const size_t array_size = 10;
int ival [array_size] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int ival2[array_size] ;
for (int ix = 0; ix != array_size; ix++)
{
ival2[ix] = ival[ix];
}
cout << "ival" << endl;
for (int i = 0; i < array_size; i++)
{
cout << ival[i];
}
cout << endl;
cout << "ival2"<<endl;
for (int i = 0; i < array_size; i++)
{
cout << ival2[i];
}
system("pause");
return 0;
}
intmain()
{
int length = 10;
vector<int> ivec;
vector<int> ivec2;
for (int i = 0; i < length; i++)
{
ivec.push_back(i);
}
ivec2 = ivec;
for (vector<int>::iterator iter =ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter;
for (vector<int>::iterator iter =ivec2.begin(); iter != ivec2.end(); ++iter)
cout << *iter;
system("pause");
return 0;
}
4.8编写程序判断两个数组是否相等,然后编写一段类似冷需比较两个vector。
#include<iostream>
#include<vector>
int main()
{
const size_t array_size = 10;
intival[array_size] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
intival2[array_size] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
intcount = 0;
for(size_t ix = 0; ix < array_size; ix++)
{
count= count + 1;
if(ival[ix] != ival2[ix])
break;
}
if(count == array_size)
cout<< "ival" << "is equal" <<"ival2";
system("pause");
return 0;
}
intmain()
{
vector<int>ivec(10, 20);
vector<int> ivec2(10, 20);
if (ivec == ivec2)
cout << "ivec" <<" is equal with " << "ivec2";
system("pause");
return 0;
}
4.9编写程序定义一个有10个int型元素的数组,并以其在数组中的位置作为各元素的初始值。
#include<iostream>
intmain()
{
const size_t array_size = 10;
int ival[array_size];
for (size_t ix = 0; ix < array_size; ix++)
ival[ix] = ix;
for (size_t ix = 0; ix < array_size; ix++)
cout << " " << ix<< " " << ival[ix];
system("pause");
return 0;
}
编写代码修改指针的值;然后再编写代码修改指针所指对象的值。
#include<iostream>
intmian()
{
int ival = 1024, ival2 = 2048;
int *pi = &ival, *pi2 = &ival2;
cout << " \"" <<"*pi" << " \" "<< *pi;
cout << " \"" <<"*pi2" << " \" "<< *pi2;
pi = pi2;
cout << " \"" <<"*pi" << " \" " << *pi;
cout << " \"" <<"*pi2" << " \" " << *pi2;
system("pause");
return 0;
}
4.15解释指针和引用的主要区别。
第一个区别是:引用总是指向某个对象,定义引用时没有初始化是错误的。第二个最重要的区别则是赋值行为的差异:给引用赋值修改的是该引用所关联的对象的值,而不是使引用与另一个对象关联。引用一经初始化,就始终指向同一个特定对象(这就是为什么引用必须在定义时初始化的原因)。
4.17已知p1与p2指向同一个数组中的元素,下面语句实现什么功能?
p1+=p2-p1;
当p1与p2具有什么值时这个语句是非法的?
把P2的值赋于p1。
此语句使得p1 也指向p2 原来所指向的元素。原则上说,只要p1 和p2 的类型
相同,则该语句始终是合法的。只有当p1 和p2 不是同类型指针时,该语句才
不合法(不能进行-操作)。
但是,如果p1 和p2 不是指向同一个数组中的元素,则这个语句的执行结果可
能是错误的。因为-操作的结果类型ptrdiff_t 只能保证足以存放同一数组中两
个指针之间的差距。如果p1 和p2 不是指向同一个数组中的元素,则-操作的结
C++Primer(4 版)习题解答
72
果有可能超出ptrdiff_t 类型的表示范围而产生溢出,从而该语句的执行结果
不能保证p1 指向p2 原来所指向的元素(甚至不能保证p1 为有效指针)。
4.17编写程序,使用指针把一个int型数组的所有元素设置为0。
#include<iostream>
int_tmain(int argc, _TCHAR* argv[]) ()
{
const size_t array_size = 10;
int a[array_size] = { 0, 1, 2, 3, 4, 5, 6, 7,8, 9 };
for (size_t ix = 0; ix < array_size; ix++)
{
cout << "original "<< a[ix]<<endl;
a[ix] = 0;
cout << "after " <<a[ix] << endl;
}
system("pause");
return 0;
}
4.19解释下列5个定义,指出其中哪些定义是非法的:
(a)int i;合法
(b)const int ic;非法,常量不可以更改,所以定义时必须初始化
(c)const int *pic 合法,指向常量的指针
(d)int *const cpi 非法,定义了指针常亮,不可以更改,必须在定义时进行初始化
(e)const int *const cpic;
因为cpic被定义为指向int型const对象的const指针,但该指针没有初始化。
4.20下列哪些初始化是合法的,为什么?
(a)int i = -1; i为整型变量,初始化值为-1;
(b)const int ic = i;ic为整型常量,由i在运行时赋值;
(c)const int *pic = &ic ;pic为指向int常量的指针,初始化指向常量ic
(d)int* const cpi = ⁣pic为指向int型数值的指针常量,不能用const int
型对象ic 的地址对其进行初始化
(e)const int *const cpic = ⁣ 合法:定义了一个指向int 型const 对象的const 指针cpic,并用ic的地址对其进行初始化。
4.25编写程序比较两个string类型的字符串,然后编写另一个程序比较两个C风格字符串的值。
#include<iostream>
#include<string>
usingnamespace std;
intmian()
{
string str = "string";
string str1 = "string1";
if (str == str1)
{
cout << "str and str1 isequal" << endl;
}
else
{
cout << "str is not equal withstr1 " << endl;
}
system("pause");
return 0;
}
intmain()
{
char *p = "string";
char *p2 = "string";
if (strcmp(p, p2) == 0)
{
cout << "str and str1 isequal" << endl;
}
else
{
cout << "str is not equal withstr1" << endl;
}
system("pause");
return 0;
}
4.26
编写程序从标准输入设备读入一个string类型的字符串。考虑如何编成实现从标准设备读入一个C风格字符串。
#include<iostream>
#include<string>
int main()
{
stringstr;
chara[10];
charc;
cin>> str ;
cout<<"str" << str << endl;
for(int i = 0; i < 10; i++)
{
cin>> c;
a[i]= c;
cout<< "a" << i << a[i];
}
//
const int str_size = 2;
charstr1[str_size];
cin>> str;
cout<< "str1" << str1;
system("pause");
return0;
}
}
4.28编写程序由标准输入设备读入的元素建立一个int的vector对象,然后动态的创建一个与该vector对象大小一致的数组,把vector对象的所有元素赋值给新数组。
#include<iostream>
#include<vector>
using namespace std;
int mian()
{
vector<int>ivec;
inti;
int*p;
int*tp;
while(cin >> i)
{
if(i == 0)
break;
else
ivec.push_back(i);
}
p =new int[ivec.size()];
tp =p;
for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++,tp++)
*tp= *iter;
for(int*q = p; q < p+ivec.size();q++)
cout<< *q << endl;
delete[]p;
system("pause");
return0;
}
4.30编写程序连接两个C风格字符串字面值,把结果存储在一个C风格字符串中,然后再编写程序连接两个string类型字符串,这两个string类型字符串与前面的C风格字符串字面值具有相同的内容。
解决This function or variable may be unsafe
http://jingyan.baidu.com/article/49711c616b8a1ffa441b7cdc.html
#include<iostream>
#include<string>
int main()
{
constchar *cp1 = "Mary and Lina";
constchar *cp2 = "are friends";
size_tlen = strlen(cp1) + strlen(cp2);
char*result_str = new char[len+1];
strcpy(result_str,cp1);
strcat(result_str,cp2);
cout<< "result_str" << result_str << endl;
delete[]result_str;
system("pause");
return0;
}
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cstring>
int main()
{
conststring str1("Mary and Lina");
conststring str2("are friends");
stringresult_str;
result_str= str1;
result_str+= str2;
cout<< result_str;
system("pause");
return0;
}
4.31编写程序从标准输入设备读入字符串,并把该字符串存放在字符数组中。描述你的程序如何处理可变长的输入。提供比你分配的数组长的字符串数据测试你的程序。
#include<iostream>
int main()
stringin_str;//用于读入字符串的string
constsize_t str_size = 10;
charresult_str[str_size + 1];
//读入字符串
cout<< "Enter a string(" << str_size <<"characters) :" << endl;
cin>> in_str;
//计算需要复制的字符的数目
size_tlen = strlen(in_str.c_str());
if(len > str_size){
len= str_size;
cout<< "string is longer than" << str_size
<< "characters and is storedonly" <<
str_size << "characters!"<< endl;
}
//复制len个字符至字符数组result_str
strncpy(result_str,in_str.c_str(), len);
//在末尾加上一个字符串(null字符)
result_str[len]= '\0';
cout<< result_str << endl;
system("pause");
return0;
}
4.33编写程序把int型vector复制给int数组。
解答:
//把int型vector复制给int型数组
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>ivec;
intival;
//输入vector元素
cout<< "Enter numbers :" << endl;
while(cin >> ival)
{
ivec.push_back(ival);
if(ival == 0)
break;
}
//创建数组
int*parr = new int[ivec.size()];
//复制元素
size_tix = 0;
for(vector<int>::iterator iter = ivec.begin();
iter!= ivec.end(); ++iter, ++ix)
parr[ix]= *iter;
for(int*p=parr; p < parr + ivec.size(); p++)
cout<< *p << endl;
delete[]parr;
//释放数组
system("pause");
return0;
}
4.34编写程序读入一组string类型的数据,并把它存储在vector中。接着,把该vector对象复制给一个指针字符数组。为vector中的每个元素创建一个新的字符数组,该vector元素的数据复制到相应的字符数组中,最后把指向该数组的指针插入字符指针数组。
#include<iostream>
#include<vector>
int mian()
{
vector<string>svec;
stringstr;
cout<<”Enterstrings ”;
while(cin>>str)
{
if(str==’o’)
break;
}
char**parr = new char*[svec.size()];
//处理vector元素
size_tix = 0;
for(vector<string>::iteratoriter=svec.begin();
iter!=svec.end();++iter,++ix)
//创建字符数组
char*p = new char[(*iter)].size()+1];
//复制vector元素的数据到字符数组
strcpy(p,(*iter).c_str());
//将指向该字符数组的指针插入到字符指针数组
parr[ix]=p;
for(intix=0;ix!=svec.size();++ix)
cout<<**parr[ix]<<endl;
delete[]parr;
return0;
}