2d-vector

initializing 2d vector using only 1 line c++

怎甘沉沦 提交于 2020-12-13 04:58:07
问题 I need to be able to initialize a 2D vector of int's in the same line in which I create it. To be more specific, I have to create a 3x2 size 2D vector and set all of it's values to 0 using only 1 line of code . Is there a way this can be done without using a for loop and several lines of code? 回答1: Try this: std::vector<std::vector<int>> twoDimVector(3, std::vector<int>(2, 0)); 回答2: If you have small 2d vectors (like as you suggested) it can be achieved (using brace-init) quity easily.

initializing 2d vector using only 1 line c++

自古美人都是妖i 提交于 2020-12-13 04:58:05
问题 I need to be able to initialize a 2D vector of int's in the same line in which I create it. To be more specific, I have to create a 3x2 size 2D vector and set all of it's values to 0 using only 1 line of code . Is there a way this can be done without using a for loop and several lines of code? 回答1: Try this: std::vector<std::vector<int>> twoDimVector(3, std::vector<int>(2, 0)); 回答2: If you have small 2d vectors (like as you suggested) it can be achieved (using brace-init) quity easily.

C++ How to dynamically create a 2D vector

家住魔仙堡 提交于 2020-01-30 08:57:05
问题 I'm trying to create an n x n vector that I can later cout as a table/matrix. Xcode points to the = in the for loop and tells me No viable overloaded '=' . I don't know what that means or how to fix it. int n=5; vector< vector<int> > row(n); for (int i=0; i<n; i++) { row[i] = new vector<int> column(n); } Also tried this, but Xcode didn't like it either and this time pointed to column and said Expected ')' : int n=5; vector< vector<int> > row; for (int i=0; i<n; i++) { row.push_back(new vector

pushing back data into 2d vector in c++

别等时光非礼了梦想. 提交于 2019-12-11 22:54:03
问题 vector <int> col(0); vector<vector<int> > row(0); for(i=0;i<10;i++) col.push_back(some integer); row.push_back(col); col.clear(); Can someone tell me what is wrong here? In the col[] vector, there is no error, but when I go to the next instruction by debug on line the before last line, just size of row[] changes to 1 from 0, but I can't see the values that it should take from the col[] vector? Edit: I tried to put debug screen's screen-shot but reputation thing happened. 回答1: There is

Some questions regarding 2d Vectors, C++

会有一股神秘感。 提交于 2019-12-11 12:45:07
问题 This 2d vector is being used to hold a game-board for minesweeper. I want to create a 2d vector of struct cell, which has several "state" variables all holding information needed to construct the game board (I am creating a basic minesweeper game to run on the command line, very rudimentary, just want to get a better grasp of classes). First of all, what am I doing wrong when trying to pass the vector to the void function? And then how would I be able to access the separate variables to read

2d vector subscripts going out of range even when the subscripts are in range

浪子不回头ぞ 提交于 2019-12-11 05:37:34
问题 So I am trying to write a simple code that will take in 2 strings from the users. It then takes the length of the strings using length() and creates a matrix (2d vector) of ints based on the lengths. I then need to set the values of the last row and last column to have values of powers of 2. If the entered strings are "happy" & "sad" the resulting matrix should be: 0 0 0 0 0 6 0 0 0 0 0 4 0 0 0 0 0 2 10 8 6 4 2 0 I am generating the matrix like this: vector<vector<int>> opt; unsigned int x, y