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=5;
    int a1[size]={0,1,2,3,4};
    int a2[size];
    for(size_t i=0; i<size; ++i)
        a2[i]=a1[i];
    system("PAUSE");
    return 0;
}

用vector实现:

#include <iostream>
#include <vector>
using namespace std;
int main( )
{
    int a[5]={1,2,3,4,5};
    vector<int> vec1(a,a+5);
    vector<int> vec2;
    for(vector<int>::iterator it=vec1.begin(); it!=vec1.end(); ++it)
        vec2.push_back(*it);
    system("PAUSE");
    return 0;
}

【习题 4.8】

编写程序判断两个数组是否相等,然后编写一段类似的程序比较两个 vector。

bool judge1(int *a, int *b, int n)
{
    for(size_t i=0; 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=0; 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[10];
    for(size_t i=0; i<10; i++) {
        a[i]=i;
        cout<<a[i]<<" ";
    }
    cout<<endl;
    system("PAUSE");
    return 0;
}

【习题 4.14】

编写代码修改指针的值;然后再编写代码修改指针所指对象的值。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main( )
{
    int *p;
    int a=1;
    int b=2;
    p=&a;
    cout<<p<<endl;
    p=&b;
    cout<<p<<endl;
    *p=12;
    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
    system("PAUSE");
    return 0;
}

【习题 4.18】

编写程序,使用指针把一个 int 型数组的所有元素设置为 0。

#include <iostream>
using namespace std;
int main( )
{
    int a[5]={1,2,3,4,5};
    for(int*p=a; p<a+5; p++)
        *p=0;
    for(int *p=a; p<a+5; p++)
        cout<<*p<<endl;
    system("PAUSE");
    return 0;
}

【习题 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 0;
}

比较两个 C 风格字符串:

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

int main( )
{
    const int size=80;
    char *s1,*s2;
    s1=new char[size];
    s2=new char[size];
    if(s1==NULL || s2==NULL) {
        cout<<"No enough memory!"<<endl;
        return -1;
    }
    cout<<"Enter two strings:"<<endl;
    cin>>s1>>s2;
    int result;
    result=strcmp(s1,s2);
    if(result>0)
        cout<<"\""<<s1<<"\""<<"is bigger than"<<"\""<<s2<<"\""<<endl;
    else if(result<0)
        cout<<"\""<<s2<<"\""<<"is bigger than"<<"\""<<s1<<"\""<<endl;
    else
        cout<<"thay are equal"<<endl;
    delete []s1;
    delete []s2;
    system("PAUSE");
    return 0;
}

【习题 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=0; i<size; i++) {
        a[i]=vec[i];
    }
    cout<<"符合要求的数组为:";
    for(int i=0; i<size; i++)
        cout<<a[i]<<endl;
    delete []a;
    system("PAUSE");
    return 0;
}

【习题 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+1];
    strcpy(res,s1);
    strcat(res,s2);
    cout<<res<<endl;
    delete []res;
    system("PAUSE");
    return 0;
}

改进后的代码:

#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 0;
}

【习题 4.31】

编写程序从标准输入设备读入字符串,并把该串存放在字符数组中。描述你的程序如何处理可变长的输入。提供比你分配的数组长度长的字符串数据测试你的程序。

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main( )
{
    string instr;
    const size_t size=10;
    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+1]=‘\0‘;
    return 0;
}

【习题 4.32】

编写程序用 int 型数组初始化 vector 对象。

#include<iostream>
#include<vector>
using namespace std;
int main( )
{
    const size_t arr_size=9;
    int int_arr[arr_size];
    cout<<"请输入:"<<arr_size<<"个元素:"<<endl;
    for(size_t i=0; i!=arr_size; i++)
        cin>>int_arr[i];
    vector<int> ivec(int_arr, int_arr+arr_size);
    system("PAUSE");
    return 0;
}

【习题 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=0;
    for(vector<int>::iterator it=ivec.begin(); it!=ivec.end(); ++it,++i)
        a[i]=*it;
    delete []a;
    system("PAUSE");
    return 0;
}

【习题 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=0;
    for(vector<string>::iterator it=svec.begin(); it!=svec.end(); it++) {
        char *p=new char[(*it).size()+1];
        strcpy(p, (*it).c_str());
        arr[i]=p;
    }
    for(i=0; i!=svec.size(); i++)
        delete []arr;
    system("PAUSE");
    return 0;
}
时间: 2024-08-02 11:21:31

C++primer习题--第4章的相关文章

C++primer习题--第3章

本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter3-ans.html,转载请注明源地址. [习题 2.11]编写程序,要求用户输入两个数——底数( base)和指数( exponent),输出底数的指数次方的结果. #include <iostream> #include <math.h> #include <string> using namespace std; int main( ) { int

C++Primer第五版习题解答---第一章

C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #include<iostream> int main() { std::cout << "hello, world" << std::endl; return 0; } 练习1.4: #include<iostream> int main() { int

C++ Primer Plus 第17章 输入,输出和文件

第17章 输入.输出和文件 1.当到达输入语句时,他将刷新输出缓冲区中当前所有的输出 2.streambuf类 提供了对缓冲区的各种操作 ios_base类表示流的一般特征 ios类基于ios_base,包含了一个指向streambuf对象的指针 ostream类从ios派生,提供可输出方法 istream类从ios派生,提供可输入方法 iostream类基于ostream和istream,继承了输入和输出方法 3.包含iostream则自动创建了8个流对象 cin 标准输入 wcin 宽字符流

C Primer Plus (第四章总结)

1.定义字符串可以直接在头文件下定义,如: #include <stdio.h> #define hello  "hello world!" 2.sizeof() 和 strlen() sizeof运算符是以字节为单位给出数据的大小,strlen()是以字符为单位给出长度. <string.h>包含许多与字符串相关的函数的原型,包括strlen() sizeof运算符提供的数据比肉眼直观的要大多一位,因为他把用来标志字符串的不可见的空字符也计算在内. 定义常量最

C++ Primer Plus 第15章 友元、异常和其他

第15章 友元.异常和其他 1.友元不仅有友元函数,还能是友元类 还可以将类中的某一个成员函数指定为另一个类的友元 尽管友元被授予从外部访问私有部门的权限,单并不与面向对象编程思想相愽,相反,它们提高了公有接口的灵活性 2.类的成员函数作为其他类的友元,涉及到类的声明顺序. 还有一个函数作为两个类的友元 这方面内容看P607-611 3.嵌套类:在另一个类中声明的类被称为嵌套类 类嵌套与包含不一样.包含意味着将一个类对象作为另一个类的成员,而对类进行嵌套不创建类成员,而是定义了一种类型. 4.对

C++ Primer 笔记 第三章

C++ Primer 第三章 标准库类型 3.1using声明 例: using namespace atd; using std::cin; 3.2string类型 初始化方式 string s1 默认构造函数,s1为空串 string s2(s1) 将s2初始化为s1的一个副本 string s3(“value”) 将s3初始化为一个字符串的副本 string s4(n, 'c') 将s4初始化为字符'c'的n个副本 getline读取整行文本 getline接受两个参数:一个是输入流对象和

《机器学习》 西瓜书习题 第 2 章

习题 \(2.1\) 数据集包含 \(1000\) 个样本, 其中 \(500\) 个正例.\(500\) 个反例, 将其划分为包含 \(70\%\) 样本的训练集和 \(30\%\) 样本的测试集用于留出法评估, 试估算共有多少种划分方式. 如果划分要保证正例和反例一样多的话, 那么划分方式数量 \(n\) 有 \[\begin{aligned} n &= C^{500\times35\%}_{500}\times C_{500}^{500\times 35\%}\&=(C^{175}_

C Primer Plus 第十一章 习题总结……2015.5.10

C Primer Plus         第五版 第十一章  字符串和字符串函数 1.没有加上字符串结尾标志 空字符'\0': 2.#include<stdio.h> int main(void) { char note[]="See you at snack bar: "; char *ptr; ptr=note; puts(ptr); puts(++ptr); note[7]='\0'; puts(note); puts(++ptr); return 0; } 3.#

c++ Primer 第五版习题答案第二章

练习2.1 Q: 类型int.long.long long和short的区别是什么,无符号和带符号类型的区别是什么?float和double的区别是什么? A:int. long. long long和short尺寸不同,表示的数据范围不同.无符号只能表示0和正数,无符号还可以表示负数.float为单精度浮点数,double为双精度,一般来说,float占4字节,double占8字节. 练习2.2 Q: 计算按揭贷款时,对于利率.本金和付款分别应选择何种数据类型?说明你的理由. A: 利率应该用