文章

C++ Primer 习题3.24

3.24

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    vector<int> x;
    int s1;
    while (cin >> s1) {
        x.push_back(s1);
    }
    for (auto i = x.begin();i < x.end()-1;++i){
        cout << *i + *(i + 1) << endl;

    }
    return 0;
}
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    vector<int> s1;
    int s2;
    while (cin >> s2) {
        s1.push_back(s2);
    }
    auto i1 = s1.begin();
    auto i2 = s1.end() - 1;
    for (;i1 < i2;++i1, --i2)
        cout << *i1 + *i2 << endl;


    return 0;
}

3.25

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    vector<int> scores(11, 0);
    int grade;
    while(cin>>grade){
        auto duanwei = scores.begin();
        if (grade <= 100)
        ++*(duanwei + grade / 10);   //单次运行的程序,duanwei初始化都是0,可是循环保存这样的话duanwei就不会一直为0,就会计算出错
            //++scores[grade/10];
        for(auto &e:scores)
        cout << e << endl;
    }
    return 0;
}

3.26
迭代器不支持加法运算

原文来自:C++ Primer 习题3.24,尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论