C ++中向量大小调整的问题

时间:2014-08-31 20:04:18

标签: c++ vector

我可能错了,但我的印象是C ++中矢量中的元素数量可以动态改变?但是,当我运行以下代码时,我得不到我期望的结果。

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector <int> vec(10);
    for(int i=0; i<10; i++){
        vec[i]=i+1;
        cout << vec[i] << endl;
    }
    cout << "the length of this vector is " << vec.size() << endl;
    for(int i=0; i<20; i++){
        vec[i]=i+1;
        cout << vec[i] << endl;
    }
    cout << "the length of this vector is " << vec.size() << endl;
}

输出看起来像

1
2
3
4
5
6
7
8
9
10
the length of this vector is 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
the length of this vector is 10

我原以为第二个vec.size()给了我二十的价值?我在C ++中寻找一个像Python中的列表一样的对象。

6 个答案:

答案 0 :(得分:2)

要调整向量的大小,您必须调用resize,访问元素out of bound是未定义的行为:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector <int> vec(10);
    for(int i=0; i<10; i++){
        vec[i]=i+1;
        cout << vec[i] << endl;
    }
    cout << "the length of this vector is " << vec.size() << endl;
    // resize here
    vec.resize(20);
    for(int i=0; i<20; i++){
        vec[i]=i+1;
        cout << vec[i] << endl;
    }
    cout << "the length of this vector is " << vec.size() << endl;
}

live example

答案 1 :(得分:2)

您确实应该使用其成员函数resize调整向量的大小,或者使用其成员函数push_back添加新元素

例如

#include <iostream>
#include <vector>


int main()
{
    const size_t N = 10; 
    std::vector <int> vec( N );

    for ( int i = 0; i < N; i++ )
    {
        vec[i] = i+1;
        std::cout << vec[i] << std::endl;
    }

    std::cout << "the length of this vector is " << vec.size() << std::endl;

    vec.resize( 2 * N );

    for ( int i = 0; i < 2 * N; i++ )
    {
        vec[i] = i+1;
        std::cout << vec[i] << std::endl;
    }

    std::cout << "the length of this vector is " << vec.size() << std::endl;
}

或者

#include <iostream>
#include <vector>


int main()
{
    const size_t N = 10; 
    std::vector <int> vec( N );

    for ( int i = 0; i < N; i++ )
    {
        vec[i] = i+1;
        std::cout << vec[i] << std::endl;
    }

    std::cout << "the length of this vector is " << vec.size() << std::endl;


    for ( int i = N; i < 2 * N; i++ )
    {
        vec.push_back( i+1 );
        std::cout << vec[i] << std::endl;
    }

    std::cout << "the length of this vector is " << vec.size() << std::endl;
}

考虑到您可以使用标准算法std::iota而不是循环。

例如

#include <iostream>
#include <vector>
#include <numeric>    

int main()
{
    const size_t N = 10; 
    std::vector <int> vec( N );

    std::iota( v.begin(), v.end(), 1 );

    for ( int x : vec ) std::cout << x << ' ';
    std::cout << std::endl;

    std::cout << "the length of this vector is " << vec.size() << std::endl;

    vec.resize( 2 * N );

    std::iota( v.begin() + N, v.end(), N + 1 );

    for ( int x : vec ) std::cout << x << ' ';
    std::cout << std::endl;

    std::cout << "the length of this vector is " << vec.size() << std::endl;
}

答案 2 :(得分:0)

您必须在矢量上调用resize()。或致电push_back()改变大小。

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector <int> vec(10);
    for(int i=0; i<10; i++){
        vec[i]=i+1;
        cout << vec[i] << endl;
    }
    cout << "the length of this vector is " << vec.size() << endl;
    vec.resize(20); //Added this line
    for(int i=0; i<20; i++){
        vec[i]=i+1;
        cout << vec[i] << endl;
    }
    cout << "the length of this vector is " << vec.size() << endl;
}

答案 3 :(得分:0)

向量不够智能,无法知道何时需要添加更多内容,因此您应该使用resize函数来扩展向量的大小,或使用push_back函数来添加每次到向量结束时都有一个新元素。

答案 4 :(得分:0)

对于&#34;任意插入&#34;,map的行为会更符合您的期望。但是,它不是一个阵列。也就是说,元素不是连续排列的。但。使用迭代器来访问它的元素,编码明智,你不会注意到很多不同。

#include <iostream>
#include <map>

int main()
{
    std::map <int, int> vec;
    for(int i=0; i<10; i++){
        vec[i]=i+1;
        std::cout << vec[i] << std::endl;
    }
    std::cout << "the length of this vector is " << vec.size() << std::endl;
    for(int i=0; i<20; i++){
        vec[i]=i+1;
        std::cout << vec[i] << std::endl;
    }
    std::cout << "the length of this vector is " << vec.size() << std::endl;
}

答案 5 :(得分:0)

您应该写如下:

int main(){ 
   vector <int> vec(10);
   for(int i=0; i<10; i++){
       vec[i]=i+1;
       cout << vec[i] << endl;
   }
   cout << "the length of this vector is " << vec.size() << endl;
   vec.resize(20);
   vec.shrink_to_fit();
   for(int i=0; i<20; i++){ 
    vec[i]=i+1;
    cout << vec[i] << endl;
   }
   cout << "the length of this vector is " << vec.size() << endl;
}