Assigning to a two dimensional vector in c++ - 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];
}
}

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.

I am not sure why this vector transpose is not working

I want to tranpose a matrix using vector?
//I am trying to 'tranpose' a vector matrix, but it's not running
#include <iostream>
#include <vector>
using namespace std;
// this will tranpose matrix
int transpose(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}}}
// this will swap the matrix
int swap_vector(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m/2; j++) {
int temp = arr[i][j];
arr[i][j] = arr[i][j-1-i];
arr[i][j-1-i] = temp;
}}}
vector<vector<int>> arrh{{1,2,3,4},{5,6,7,8},
{9,10,11,12},
{13,14,15,16} };
int main() {
transpose(arrh);
swap_vector(arrh);
for (int i=0; i<arrh.size(); i++) {
for (int j=0; j<arrh[0].size(); j++) {
cout << arrh[i][j] << " ";}
cout << std::endl;}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
// this will tranpose matrix
int transpose(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = i; j < m; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
// this will swap the matrix
int swap_vector(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m/2; j++) {
int temp = arr[i][j];
arr[i][j] = arr[i][m-1-j];
arr[i][m-1-j] = temp;
}
}
}
vector<vector<int>> arrh{{1,2,3,4},{5,6,7,8},
{9,10,11,12},
{13,14,15,16} };
int main() {
transpose(arrh);
swap_vector(arrh);
for (int i=0; i<arrh.size(); i++) {
for (int j=0; j<arrh[0].size(); j++) {
cout << arrh[i][j] << " ";
}
cout << std::endl;
}
return 0;
}

Heap corruption detected error in C++

Here is my code:
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
int** arr=NULL;
int num=0;
cin >> num;
int* big=NULL;
arr = new int*[num];
for (int i = 0; i < num; i++) {
arr[i] = new int[5];
}
big = new int[num];
for (int i = 0; i < num; i++) {
for (int j = 0; j < 5; j++) {
while (1) {
cin >> arr[i][j];
if (arr[i][j] >= 0 && arr[i][j] < 100)
break;
}
}
}
for (int i = 0; i < 5; i++) {
big[i] = 0;
}
for (int i = 0; i < num; i++) {
for (int j = 0; j < 5; j++) {
if (big[i] < arr[i][j]) {
big[i] = arr[i][j];
}
}
}
for (int i = 0; i < num; i++) {
cout << "Case #" << i + 1 << ": " << big[i] << endl;
}
delete[]big;
for (int i = num-1; i>=0; i--) {
delete[]arr[i];
}
delete[]arr;
return 0;
}
When I run this code, it says that there are heap corruption error (heap corruption detected). I think it means that there are some errors at 'new' or 'delete' parts in my codes, but I cannot find them. I hope someone to answer. Thanks.
Error is here:
big = new int[num];
...
for (int i = 0; i < 5; i++) {
big[i] = 0;
}
So when you have num less than 5 you are writing outside the array.
Anyway you are using C++ so use vector for such tasks.
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
int main() {
vector<vector<int>> arr;
int num=0;
cin >> num;
arr.resize(num, vector<int>(5));
for (auto &row : arr) {
for (auto &cell : row) {
while (1) {
cin >> cell ;
if (cell >= 0 && cell < 100)
break;
}
}
}
vector<int> big(arr.size());
for (int i = 0; i < arr.size(); i++) {
for (auto &cell : arr[i]) {
if (big[i] < cell) {
big[i] = cell;
}
}
}
for (int i = 0; i < num; i++) {
cout << "Case #" << i + 1 << ": " << big[i] << endl;
}
return 0;
}
In many places in your code, you're indexing your big array using indexes from 0 to 5, while the array is allocated using user input, if user input was 4 for example, your code is undefined behavior.
If you're using c++, you shouldn't be manually allocating the arrays, use std::vector instead, it will take care of managing memory for you, so you don't have to new and delete memory yourself.
With std::vector, your code would look somewhat like this.
std::vector<std::vector<int>> arr;
std::vector<int> big;
cin>>num;
arr.resize(num, std::vector<int>(5));
big.resize(5);
You will also be able to use at method to access elements while bound-checking, and size method to get the number of elements of the array.

2D vector push_back

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;
}
}
}

C++: Why is the created vector not passing to the next set of for loops?

I have it so that the user defines the size of the vector and then a vector is filled. Then that vector is sorted using bubble sort (homework assignment). However, when the "sorted" vector is displayed, there are different numbers in it than what were in the original creation. How do I first create the vector, display it, then sort it and display it??
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
size = data.size();
//Sorting
void bubbleSort(vector<int> & data);
{
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
First off:
make sure to include all necessary header files, e.g. stdlib.h for your used rand() function.
get rid of all unused variables, like average, median and size.
declare your bubbleSort function outside of main function, and add additional checkup code to prevent sort if list has not more than one element.
The sort problem is related to this code snippet of yours:
for (int i = 0; i<size -1 - k; i++) { ... }
Simply remove -1
To fix your sort problem, and for better output, use following code:
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
using namespace std;
void bubbleSort(vector<int>& data)
{
if(data.size() <= 1)
return;
for(int i=1; i<data.size(); i++)
{
for(int j=0; j<data.size()-i; j++)
{
if(data.at(j) > data.at(j+1))
{
int temp = data.at(j+1);
data.at(j+1) = data.at(j);
data.at(j) = temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout << "Vector Length?: ";
cin >> n;
// Filling vector
for(int i=0; i<n; i++)
data.push_back(rand()%10+1);
cout << "Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
// Sorting
bubbleSort(data);
cout << endl << "Sorted Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
return 0;
}
Hope this helps ;)
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
void printVector(vector<int> & data)
{
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}
//Sorting
void bubbleSort(vector<int> & data);
{
size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
}
}
}
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
printVector(data);
bubbleSort(data);
printVector(data);
}
If you are planning to define function void bubbleSort(vector & data) later you need to declare it before calling it.\
void bubbleSort(vector<int> & data);
int main()
{
// Here your code
bubbleSort(data);
//Here your code
}
You need to define variables only just before you need it. And if you declare and never use it, you will get unused variable warnings. So better you can comment all these variables. You can un-comment whenever you need.
//double average=0.0;
//int median = 0;
You should call your function bubbleSort() from main() as follows:
bubbleSort(data);
You are not using the iterator indexes properly to sort the elements of vector. If you change your function bubbleSort as follows it will work
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
complete program for your reference:
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
int main()
{
int n;
//double average=0.0;
//int median = 0;
//double size = 0.0;
//int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
// int n =10;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
bubbleSort(data);
std::cout<<"sorted vector"<<"\n";
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}