C++primer习题--第4章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter4-ans.html,转载请注明源地址。
【习题 4.7】
编写必要的代码将一个数组赋给另一个数组,然后把这段代码改用 vector 实现。 考虑如何将一个 vector 赋给另一个 vector。
用数组实现:
#include <iostream>
using namespace std;
int main( )
{
const size_t size=;
int a1[size]={,,,,};
int a2[size];
for(size_t i=; i<size; ++i)
a2[i]=a1[i];
system("PAUSE");
return ;
}
用vector实现:
#include <iostream>
#include <vector>
using namespace std;
int main( )
{
int a[]={,,,,};
vector<int> vec1(a,a+);
vector<int> vec2;
for(vector<int>::iterator it=vec1.begin(); it!=vec1.end(); ++it)
vec2.push_back(*it);
system("PAUSE");
return ;
}
【习题 4.8】
编写程序判断两个数组是否相等,然后编写一段类似的程序比较两个 vector。
bool judge1(int *a, int *b, int n)
{
for(size_t i=; i<n; i++) {
if(a[i]!=b[i])
return false;
}
return true;
}
比较vector:
bool judge2(vector<int> a, vector<int> b)
{
for(vector<int>::size_type it=; it<a.size(); it++) {
if(a[it]!=b[it])
return false;
}
return true;
}
【习题 4.9】
编写程序定义一个有 10 个 int 型元素的数组,并以其在数组中的位置作为各元素的初值。
#include <iostream>
using namespace std;
int main( )
{
int a[];
for(size_t i=; i<; i++) {
a[i]=i;
cout<<a[i]<<" ";
}
cout<<endl;
system("PAUSE");
return ;
}
【习题 4.14】
编写代码修改指针的值;然后再编写代码修改指针所指对象的值。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main( )
{
int *p;
int a=;
int b=;
p=&a;
cout<<p<<endl;
p=&b;
cout<<p<<endl;
*p=;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
system("PAUSE");
return ;
}
【习题 4.18】
编写程序,使用指针把一个 int 型数组的所有元素设置为 0。
#include <iostream>
using namespace std;
int main( )
{
int a[]={,,,,};
for(int*p=a; p<a+; p++)
*p=;
for(int *p=a; p<a+; p++)
cout<<*p<<endl;
system("PAUSE");
return ;
}
【习题 4.25】
编写程序比较两个 string 类型的字符串,然后编写另一个程序比较两个 C 风格字符串的值。
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string s1,s2;
cout<<"输入两个字符串:\n";
cin>>s1>>s2;
if(s1>s2)
cout<<"\""<<s1<<"\""<<"is bigger than"<<"\""<<s2<<"\""<<endl;
else if(s1>s2)
cout<<"\""<<s2<<"\""<<"is bigger than"<<"\""<<s1<<"\""<<endl;
else
cout<<"thay are equal"<<endl;
system("PAUSE");
return ;
}
比较两个 C 风格字符串:
#include <iostream>
#include <string>
#include <vector>
using namespace std; int main( )
{
const int size=;
char *s1,*s2;
s1=new char[size];
s2=new char[size];
if(s1==NULL || s2==NULL) {
cout<<"No enough memory!"<<endl;
return -;
}
cout<<"Enter two strings:"<<endl;
cin>>s1>>s2;
int result;
result=strcmp(s1,s2);
if(result>)
cout<<"\""<<s1<<"\""<<"is bigger than"<<"\""<<s2<<"\""<<endl;
else if(result<)
cout<<"\""<<s2<<"\""<<"is bigger than"<<"\""<<s1<<"\""<<endl;
else
cout<<"thay are equal"<<endl;
delete []s1;
delete []s2;
system("PAUSE");
return ;
}
【习题 4.28】
编写程序由从标准输入设备读入的元素数据建立一个 int 型 vector 对象,然后动态创建一个与该 vector 对象大小一致的数组,把 vector 对象的所有元素复制给新数组。
#include <iostream>
#include <string>
#include <vector>
using namespace std; int main( )
{
vector<int> vec;
int n;
cout<<"请输入数字:"<<endl;
while(cin>>n)
vec.push_back(n);
int size=vec.size();
int *a=new int[size];
for(vector<int>::size_type i=; i<size; i++) {
a[i]=vec[i];
}
cout<<"符合要求的数组为:";
for(int i=; i<size; i++)
cout<<a[i]<<endl;
delete []a;
system("PAUSE");
return ;
}
【习题 4.30】
编写程序连接两个 C 风格字符串字面值,把结果存储在一个 C 风格字符串中。然后再编写程序连接两个 string 类型字符串,这两个 string 类型字符串与前面 的 C 风格字符串字面值具有相同的内容。
#include <iostream>
#include <cstring>
using namespace std;
int main( )
{
const char *s1="hello ";
const char *s2="world.";
size_t len=strlen(s1)+strlen(s2);
char *res=new char[len+];
strcpy(res,s1);
strcat(res,s2);
cout<<res<<endl;
delete []res;
system("PAUSE");
return ;
}
改进后的代码:
#include <iostream>
#include <string>
using namespace std;
int main( )
{
const string s1("hello ");
const string s2("world.");
string res;
res=s1;
res+=s2;
cout<<res<<endl;
system("PAUSE");
return ;
}
【习题 4.31】
编写程序从标准输入设备读入字符串,并把该串存放在字符数组中。描述你的程序如何处理可变长的输入。提供比你分配的数组长度长的字符串数据测试你的程序。
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main( )
{
string instr;
const size_t size=;
char restr[size];
cout<<"请输入字符串(<="<<size<<"个字符):"<<endl;
cin>>instr;
size_t len=strlen(instr.c_str());
if(len>size) len=size;
strncpy(restr,instr.c_str(),len);
restr[len+]='\0';
return ;
}
【习题 4.32】
编写程序用 int 型数组初始化 vector 对象。
#include<iostream>
#include<vector>
using namespace std;
int main( )
{
const size_t arr_size=;
int int_arr[arr_size];
cout<<"请输入:"<<arr_size<<"个元素:"<<endl;
for(size_t i=; i!=arr_size; i++)
cin>>int_arr[i];
vector<int> ivec(int_arr, int_arr+arr_size);
system("PAUSE");
return ;
}
【习题 4.33】
编写程序把 int 型 vector 复制给 int 型数组。
#include<iostream>
#include<vector>
using namespace std;
int main( )
{
vector<int> ivec;
int n;
cout<<"请输入数字:"<<endl;
while(cin>>n)
ivec.push_back(n);
int *a=new int[ivec.size()];
size_t i=;
for(vector<int>::iterator it=ivec.begin(); it!=ivec.end(); ++it,++i)
a[i]=*it;
delete []a;
system("PAUSE");
return ;
}
【习题 4.34】
编写程序读入一组 string 类型的数据,并将它们存储在 vector 中。接着,把该 vector 对象复制给一个字符指针数组。为 vector 中的每个元素创建一个新的字符数组,并把该 vector 元素的数据复制到相应的字符数组中,最后把指向 该数组的指针插入字符指针数组。
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
int main( )
{
vector<string> svec;
string str;
cout<<"请输入字符串:"<<endl;
while(cin>>str)
svec.push_back(str);
char **arr = new char*[svec.size()];
size_t i=;
for(vector<string>::iterator it=svec.begin(); it!=svec.end(); it++) {
char *p=new char[(*it).size()+];
strcpy(p, (*it).c_str());
arr[i]=p;
}
for(i=; i!=svec.size(); i++)
delete []arr;
system("PAUSE");
return ;
}
C++primer习题--第4章的更多相关文章
- C++primer习题--第1章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter1-ans.html,转载请注明源地址. [习题 1.3] 编一个程序,在标准输出 ...
- C++primer习题--第3章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter3-ans.html,转载请注明源地址. [习题 2.11]编写程序,要求用户输入 ...
- 《C++Primer》第五版习题答案--第一章【学习笔记】
C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...
- 《C++Primer》第五版习题答案--第二章【学习笔记】
C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...
- 《python核心编》程课后习题——第三章
核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...
- C Primer Plus_第6章_循环_编程练习
1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- [C++ Primer Plus] 第10章、对象和类(二)课后习题
1. bank.h #include <string> using namespace std; class BankAccount { private: std::string m_na ...
随机推荐
- thinkphp5.0架构总览
ThinkPHP5.0应用基于MVC(模型-视图-控制器)的方式来组织. MVC是一个设计模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型(M).视图(V) ...
- windows关闭aslr办法
关闭aslr方便调试分析. 转:https://www.52pojie.cn/thread-377450-1-1.html windows关闭aslr办法 如 @Hmily 前辈所说, Window ...
- CodeForces 909D Colorful Points
题解: 暴力,模拟. 把字符串压缩一下,相同的处理成一位,记录下个数,然后暴力模拟即可. #include <bits/stdc++.h> using namespace std; con ...
- IDC、ICP、ISP区别
ICP( Internet Content Provider):网络内容服务商,即向广大用户综合提供互联网信息业务和增值业务的电信运营商.其必须具备的证书即为ICP证,如运营一个网站,需要进行备案获取 ...
- Vim的一些补充
查看当前路径 :pwd 切换当前路径到当前打开文件所在路径 :cd %:p:h 解释: %表示当前文件名,%:p表示包含文件名的全部路径,%:p:h表示文件所在路径(head of the full ...
- Redis学习篇(六)之ZSet类型及其操作
ZADD 作用: 将元素及其分数添加到集合中 语法: ZADD key score membre [score member] 当集合元素已经存在时,再次添加会更新其分数 当score是 +inf 时 ...
- MySQL笔记(四)之内建函数
AVG() 函数 AVG 函数返回数值列的平均值.NULL 值不包括在计算中. 语法: SELECT AVG(列) FROM 表: COUNT() 函数 COUNT() 函数返回匹配指定条件的行数. ...
- BZOJ4554 HEOI2016游戏
网络流. 我一开始傻傻的将每条边与每一列连边,边权为联通块树,但是这样做是错的因为我们就在跑网络流中会忽略掉边和列的关系. 我们在做网络流题时一定要注意题目中给出的限制条件,一定要以限制条件建图!!! ...
- BZOJ 2434 [Noi2011]阿狸的打字机(AC自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2434 [题目大意] 给出一个打印的过程,'a'-'z'表示输入字母,P表示打印该字符串 ...
- 【动态规划/多重背包问题】POJ2392-Space Elevator
方法同POJ1014-Dividing,唯一不同点在于每一种block有最大限定高度a,故要以a为关键字进行排序,使得最大高度小的在前,否则最大高度小的再后可能放不上去. #include<io ...