2D vector push_back - c++

I have the code below and I'm strugling to add values to the vector. The end goal is to itterate through a list and for each itteration add a value to 2 rows of a vector but I'm strugling to understand how to push_back to a 2d vector.
std::vector<std::vector<int> >nns;
int i = 5;
nns.push_back(i, i);
for(int i = 0; i <nns.size(); i++)
{
for(int j = 0; j < nns[i].size(); j++)
{
std::cout << nns[i][j] << std::endl;
}
}
How would I add one column to this vector?
so
vector[0][0] = 0
vector[1][0] = 0?

Answer provided by Algirdas Works perfectly.
#include <iostream>
#include <vector>
using namespace std;
int main() {
std::vector<std::vector<int> > nns;
int i = 5;
nns.push_back(std::vector<int>{i});
for (int i = 0; i < nns.size(); i++) {
for (int j = 0; j < nns[i].size(); j++) {
std::cout << nns[i][j] << std::endl;
}
}
}

Related

How to compare elements of a vector with elements of a vector of vectors?

My intention is after pushing pair to vector temp_pairs, compare it's 2 elements with the elements of the vector of vectors jumpers, and if one element of temp_pairs is equal to any element of jumpers .clear() the temp_pairs.
The following code works, except for the 2 first pairs.
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
typedef std::vector<int> vector_i;
typedef std::vector<char> vector_c;
vector_c control {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','w','y','z',' ','.'};
std::vector<vector_c> jump_input();
int main()
{
jump_input();
return 0;
}
std::vector<vector_c> jump_input()
{
std::vector<vector_c> jumpers;
vector_c temp_pairs(2);
int jump_count = 0;
char in;
vector_c pair;
do
{
for (int i = 0; i < jumpers.size(); i++)
{
for (int j = 0; j < jumpers[i].size(); j++)
{
std::cout << jumpers[i][j];
}
std::cout << " ";
}
for (int i = 0; i < 2; i++)
{
std::cout << "\n" << "Enter the " << i+1 << " of the pair number "<< jumpers.size()+1 << "\n";
std::cin >> in;
for (int j = 0; j < control.size(); j++)
{
if (in == control[j])
{
pair.push_back(in);
}
}
}
if (pair.size()>1)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < control.size(); j++)
{
if (pair[i] == control[j])
{
temp_pairs.push_back(pair[i]);
}
}
}
}
// where the comparation happens
for (int i = 0; i < jumpers.size(); i++)
{
for (int j = 0; j < 2; j++)
{
if (jumpers[i][j] == temp_pairs[j])
{
temp_pairs.clear();
}
}
}
if (!temp_pairs.empty())
{
jumpers.push_back(temp_pairs);
jump_count += 1;
}
temp_pairs.clear();
pair.clear();
in='\0';
} while (jump_count<21);
return jumpers;
}
Terminal image
enter image description here
I tried series of if statements and for loops, and this is the best result i code reach.

Assigning to a two dimensional vector in c++

#include<bits/stdc++.h>
using namespace std;
main()
{
vector<vector<int> > v;
for(int i = 0;i < 3;i++)
{
vector<int> temp;
for(int j = 0;j < 3;j++)
{
temp.push_back(j);
}
//cout<<typeid(temp).name()<<endl;
v[i].push_back(temp);
}
}
I am trying to assign to a two dimensional vector. I am getting the following error
No matching function for call to
std ::vector<int>::push_back(std::vector<int> &)
Problem: Your vector v is empty yet and you can't access v[i] without pushing any vector in v.
Solution: Replace the statement v[i].push_back(temp); with v.push_back(temp);
You can follow this process :
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<vector<int> > v;
for(int i = 0;i < 3;i++)
{
vector<int> temp;
for(int j = 0;j < 3;j++)
{
temp.push_back(j);
}
//cout<<typeid(temp).name()<<endl;
v.push_back(temp);
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout << v[i][j] << " ";
}
cout << endl;
}
}
v[0] is empty, you should use v.push_back(temp);
You could use at approach to avoid this error:
for(int i = 0; i < 3; i++){
vector <vector <int> > v;
vector <int> temp;
v.push_back(temp);
v.at(COLUMN).push_back(i);
}
Then you could access it through:
v.at(COLUMN).at(ROWS) = value;
v[i].push_back(temp);
should be
v.push_back(temp);
v is type of std::vector<vector<int>>, v[i] is type of std::vector<int>
You should use v instead of v[i]. (C++11)
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
vector<vector<int> > v;
for(int i = 0;i < 3;i++)
{
vector<int> temp;
for(int j = 0;j < 3;j++)
{
temp.push_back(j);
}
v.push_back(temp);
}
for (auto element: v) {
for (auto atom: element) {
cout << atom << " ";
}
cout << "\n";
}
return 0;
}
Think of it this way: "How do I insert a variable temp of type T into my vector of std::vector<T>?" In your case it is:
v.push_back(temp);
T itself being a vector doesn't make a difference. Then to print out your vector (of vectors) you can use two for loops:
for (std::size_t i = 0; i < v.size(); i++){
for (std::size_t j = 0; j < v[i].size(); j++){
std::cout << v[i][j] << ' ';
}
std::cout << std::endl;
}
Just do it...
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<vector<int> > v;
for(int i = 0; i < 3; i++)
{
vector<int> temp;
for(int j = 0; j < 3; j++)
{
temp.push_back(j);
}
v.push_back(temp);//use v instead of v[i];
}
}

vector array - sorting failure

I have problem with sorting an array. I don't know why my codes does not sort an array properly. I am new in programming so be gentle on me. Here's a code. Also other functions like merge or quick sort does not work too. Thanks in advance for answer.
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iterator>
std::vector<int> bubbleSort(std::vector<int>& Array)
{
for (unsigned int j = 1; j < Array.size() - 1; ++j)
{
for (unsigned int i = 0; i < Array.size() - 1; i++)
{
if (Array[i] > Array[++i])
{
std::swap(Array[i], Array[++i]);
}
}
}
return Array;
}
int main()
{
for (int i = 0; i < 10;i++)
{
int N; //array size
srand(std::time(NULL));
std::cout << " array size: ";
std::cin >> N;
std::vector <int> Array;
//fill array
for (int i = 1; i <= N; i++)
Array.push_back(i);
for (int i = N - 1; i > 0; i--)
{
int j = rand() % i;
std::swap(Array[i], Array[j]);
}
bubbleSort(Array);
for (unsigned int i = 0; i < Array.size(); i++)
{
std::cout << Array.at(i) << std::endl;
}
}
system("pause");
}
It looks like you're unintentionally incrementing i within the loop. Don't use ++i, use i+1 instead. Also change your loop termination condition to just i < Array.size() instead of i < Array.size() - 1

multidimensional array function outputting garbage?

I have this function meant to initialize a multidimensional 2d (6x6) array to zero. I call the function in main using cout to test it and it outputs garbage. Please help. Thanks!
int** initializeArray(void)
{
typedef int* rollArray; //this line is actually outside of the function in my
//program
int i, j;
rollArray *m = new rollArray[6];
for (i = 0; i < 6; i++)
m[i] = new int[6];
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++)
m[i][j] = 0;
return m;
}
If the value 6 is known at compile-time, I would suggest using std::array in a nested fashion. For example:
#include <array>
#include <iostream>
int main()
{
std::array<std::array<int,6>,6> a = {0};
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
std::cout << a[i][j] << std::endl; // Prints 0.
}
}
return 0;
}
In fact, you won't even need to create a function to initialize your array. Declare your nested array and you are good to go. (If you don't know the dimension at compile-time, you could use std::vector in a similar fashion.)
The problem is with your test.
How can you mess up such a simple test? Just use:
int ** a = initializeArray();
int i,j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 6; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}

How to sort elements into C++ matrix?

I'm new to C++ programming. I need to sort this matrix:
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
Mat10 a;
fillRand(a, 5, 5);
prnMat(a, 5, 5);
cout << endl;
return 0;
}
void fillRand(Mat10 m, int n, int k) {
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
m[i][j] = rand() % 1000;
}
void prnMat(Mat10 a, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cout << setw(8) << a[i][j];
cout << endl;
}
}
I need to sort the matrix from the beginning from the beginning. The smallest value must be at the beginning of the of the first column. The next must be below it and so on. The result must be sorted matrix - the smallest number must be at the beginning of the left column - the biggest value must be at the end of the matrix. Would you please help to solve the problem?
EDIT
Maybe I found possible solution:
void sort(int pin[10][2], int n)
{
int y,d;
for(int i=0;i<n-1;i++)
{
for(int j=0; j<n-1-i; j++)
{
if(pin[j+1][1] < pin[j][1]) // swap the elements depending on the second row if the next value is smaller
{
y = pin[j][1];
pin[j][1] = pin[j+1][1];
pin[j+1][1] = y;
d = pin[j][0];
pin[j][0] = pin[j+1][0];
pin[j+1][0] = d;
}
else if(pin[j+1][1] == pin[j][1]) // else if the two elements are equal, sort them depending on the first row
{
if(pin[j+1][0] < pin[j][0])
{
y = pin[j][1];
pin[j][1] = pin[j+1][1];
pin[j+1][1] = y;
d = pin[j][0];
pin[j][0] = pin[j+1][0];
pin[j+1][0] = d;
}
}
}
}
}
But since I'm new to programming I don't understand is this the solution?
Here is a simple example for you:
#include <vector>
#include <algorithm>
using namespace std;
//This is the comparation function needed for sort()
bool compareFunction (int i,int j)
{
return (i<j);
}
int main()
{
//let's say you have this matrix
int matrix[10][10];
//filling it with random numbers.
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
matrix[i][j] = rand() % 1000;
//Now we get all the data from the matrix into a vector.
std::vector<int> vect;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
vect.push_back(matrix[i][j]);
//and sort the vector using standart sort() function
std::sort( vect.begin(), vect.end(), compareFunction );
//Finally, we put the data back into the matrix
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
matrix[i][j] = vect.at(i*10 + j);
}
After this, the matrix will be sorted by rows:
1 2
3 4
If you want it to be sorted by cols:
1 3
2 4
You need to replace matrix[i][j] in the last cycle only with matrix[j][i]
If you need to read about the the sort() function, you can do it here
Hope this helps.
You can simply call std::sort on the array:
#include <algorithm> // for std::sort
int main() {
int mat[10][10];
// fill in the matrix
...
// sort it
std::sort(&mat[0][0], &mat[0][0]+10*10);
}