C ++ 2维向量

时间:2018-11-08 01:59:53

标签: c++

我目前正在学习C ++课程,我正在学习向量。下面是我编写的代码,在其中将值分配给不同的向量。一切正常,直到我到达必须声明一个空的二维向量的部分为止。下面的问题说明以及使用XCode的代码。 (我尝试通过终端进行编译,结果相同)

/* Section 7
 Challange

 Write a C++ program as follows:

 Declare 2 empty vectors of intergers named vector1 and vector2.


 Add 10 and 20 to vector1 dynamically using push_back
 Display the elements in vector1 using the at() method as well as its size
 using the size() method.

 Add 100 and 200 to vector2 dynamically using push_back
 Display the elements in vector2 using the at() method as well as its size
 using the size() method.

 Decalre an empty 2D vector called vector_2d
 TIP: A vector inside a vector.

 Add vector1 to vector_2d dynamically using push_back
 Add vector2 to vector_2d dynamically using push_back

 Display the elements in vector_2d using the at() method

 Change vector1.at(0) to 1000.

 Display the elements in vector_2d again using the at() method

 Display the elements in vector1
 */

#include <iostream>
#include <vector>



int main(){


    std::vector <int> vector1;
    std::vector <int> vector2;

    int vector1_1addition;
    int vector1_2addition;

    std::cin >> vector1_1addition; //add 10
    std::cin >> vector1_2addition; //add 20

    vector1.push_back (vector1_1addition);
    vector1.push_back (vector1_2addition);

    std::cout << "The numbers in vector 1 are: " << vector1.at(0) <<" " << vector1.at(1) <<std::endl;
    std::cout << "The size of the vector 1 is: " << vector1.size() << std::endl;

    int vector2_1addition;
    int vector2_2addition;

    std::cin >> vector2_1addition; //add 100
    std::cin >> vector2_2addition; //add 200

    vector2.push_back (vector2_1addition);
    vector2.push_back (vector2_2addition);

    std::cout << "The numbers in vector 2 are: " << vector2.at(0) << " " << vector2.at(1) <<std::endl;
    std::cout << "The size of vector 2 is: " << vector2.size() << std::endl;

    std::vector <std::vector <int> > vector_2d;

    std::cout << "The elements in the 2 dimensional vector are: " << &vector_2d.at(0) << &vector_2d.at(1) << std::endl;



    vector1.at(0) = 1000;

    std::cout << " The new elements in the 2 dimensional vector are: " << &vector_2d.at(0) << &vector_2d.at(1) << std::endl;


    std::cout << " The new elemenrs in vector 1 are: " << vector1.at(0) << vector1.at(1) << std::endl;



    return 0;
}

2 个答案:

答案 0 :(得分:0)

问题是要打印尚不存在的二维向量的值,在打印它们之前,需要使用push_back对其添加值来声明它们。

答案 1 :(得分:0)

伟大的乌迪米课程!

    在声明vector_2d之后,需要添加
  1. vector_2d.pushback(vector1);vector_2d.pushback(vector2);

  2. 您还需要更改:

    std::cout << "The elements in the 2 dimensional vector are: " << **&vector_2d.at(0)** 
    << **&vector_2d.at(1)** << std::endl;
    

    AND:

    std::cout << " The new elements in the 2 dimensional vector are: " << 
    **&vector_2d.at(0)** << **&vector_2d.at(1)** << std::endl;
    

    以下与2d向量输出有关的代码:

    std::cout << "The elements in the 2 dimensional vector are: " << 
    &vector_2d.at(0).at(0) << &vector_2d.at(1).at(0) << std::endl;
    

    AND:

    std::cout << " The new elements in the 2 dimensional vector are: " << 
    &vector_2d.at(0) << &vector_2d.at(1) << std::endl;
    

由于二维向量包含2行和2列,因此您需要像这样输出vector of vector of ints

此外,您需要为第二行添加另一个输出。

Demo