c++14日目

5484 ワード

《c++ primer, 5E》
91ページから94ページ、メモ:
1、vectorがサポートする操作.
v.empty()、v.size()、v.push_back(t)、v[n]
 
2、下付き文字でアクセスしようとするvector要素はコンパイラに発見されず、
実行時に予知できない値を生成します.
 
3、以下の表の合法的な有効な手段を確保する:できるだけ範囲forを使用する
 
4、vectorの小結:vectorの初期化、vectorがサポートする操作、vectorの遍歴とランダムアクセス
 
問題:
 
レッスン後の練習:
練習3.16
正しい.
検証(10,「hi」)と{10,「hi」)は等価であり,
{10}と(10)の効果はvector要素のタイプに依存します.
 
練習3.17
#include
using std::cin;
using std::cout;
using std::endl;
#include
using std::vector;
#include<string>
using std::string;
int main()
{
    string word;
    vector<string> svec;
    while(cin >> word){
        svec.push_back(word);
    }
    for(auto &word: svec){
        for(auto &ch: word){
            ch = toupper(ch);
        }
        cout << word << endl;
    }
    return 0;
}

 
練習3.18
非合法.不正アクセス.ivecに変更する.push_back(42);
 
練習3.19
どうせ一番いいのはvector v(10,42);
 
練習3.20
1
#include
using std::cin;
using std::cout;
using std::endl;
#include
using std::vector;
#include<string>
using std::string;
int main()
{
    int temp;
    vector<int> v;
    while(cin >> temp){
        v.push_back(temp);
    }
    //    
    for(decltype(v.size()) index = 0;
        index != v.size()-1; ++index)
        cout << v[index]+ v[index+1] << endl;
    return 0;
}

2
#include
using std::cin;
using std::cout;
using std::endl;
#include
using std::vector;
#include<string>
using std::string;
int main()
{
    int temp;
    vector<int> v;
    while(cin >> temp){
        v.push_back(temp);
    }
    //    
    decltype(v.size()) head = 0;
    auto tail = v.size()-1;    
    for(;
    head <= tail; ++head, --tail){
        cout << v[head]+v[tail] << endl;
    }
    return 0;
}

 
転載先:https://www.cnblogs.com/xkxf/p/6392499.html