c++Vectorベクトルコンテナ

2101 ワード

Vectorを使用するには、まずヘッダファイルで#includeを定義する必要があります.
1:インスタンスによる値の割り当て、追加、削除の方法
#include 
#include
using namespace std;
int main()
{
    vector v (10,14.12);//      10 double         ,          14.12
    v[5]=10.14;	// v[5]      
    v[9]=78.10;// v[9]      
    vector::iterator it;    //       it
    for(it=v.begin();it!=v.end();it++)  //                     
    {
        cout << *it << endl;//  10 double         
    }
    cout<

コード実行結果:
14.12 14.12 14.12 14.12 14.12 10.14 14.12 14.12 14.12 78.1
14.12 14.12 1 14.12 14.12 14.12 10.14 14.12 14.12 14.12 78.1 2 5 3
14.12 14.12 10.14 14.12 14.12 14.12 78.1 2 5 3
0
Process returned 0 (0x0)   execution time : 0.065 s Press any key to continue.
コード実行結果
2:逆配列
#include 
#include
#include
using namespace std;

int main()
{
    vectorv(10,1); //          10      1 v    
    vector::iterator it;   //       it
    for(int i=0;i<10;i++)   // v          9~0;
    {
        v[i]=i;
    }
    for(it=v.begin();it!=v.end();it++)  //    v    
    {
        cout<

実行結果:
0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
Process returned 0 (0x0)   execution time : 0.016 s Press any key to continue.
 
3:sortソート(デフォルトは昇順)
#include 
#include
#include
using namespace std;
int main()
{
    vectorv(10,1); //          10      1 v    
    vector::iterator it;   //       it

    for(int i=0;i<10;i++)   // v          9~0;
    {
        v[i]=i;
    }
    sort(v.begin(),v.end());    //sort                    
    for(int j=0;j<10;j++)
    {
        cout<=0;k--)//for            
    {
        cout<