「std::vector シンプルな例」の版間の差分

提供: C++入門
移動: 案内検索
(Daemon がページ「Std::vector シンプルな例」を「std::vector シンプルな例」に移動しました)
(関連項目)
行87: 行87:
  
 
== 関連項目 ==
 
== 関連項目 ==
 
 
* [[std::vector]]
 
* [[std::vector]]
 +
* [[auto]]
 +
* [[for]]
 +
* [[C++11]]
 +
* [[ラムダ式]]
 +
* [[std::for_each]]
 
* [[C++ライブラリ]]
 
* [[C++ライブラリ]]

2013年12月28日 (土) 00:54時点における版


概要

std::vectorのシンプルな使用例です。

std::vector のシンプルな例

ソースコード vector_int_1.cpp

#include <iostream>
#include <vector>
 
int main(int argc, char const* argv[])
{
        std::vector<int>        v;
 
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
 
        for(std::vector<int>::iterator it = v.begin(); it != v.end(); it++) {
                std::cout << *it << std::endl;
        }
        return 0;
}

コンパイル

g++  vector_int_1.cpp -o vector_int_1

実行例

% ./vector_int_1
1
2
3


std::vectorを添え字で扱う例

ソースコード vector_int_2.cpp

#include <iostream>
#include <vector>
 
int main(int argc, char const* argv[])
{
        std::vector<int>        v;
 
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
 
        for(unsigned int i = 0; i < v.size(); ++i) {
                std::cout << v[i] << std::endl;
        }
        return 0;
}

コンパイル

g++  vector_int_2.cpp -o vector_int_2

実行例

% ./vector_int_2
1
2
3

関連項目