2D vector segmentation fault - c++

The following code is giving me a segmentation fault and I don't understand the problem. I think there's some mistake in the way I've used vectors but I don't know what that is. Please help.
#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int T;
vector<int>n,m;
vector<vector<int> >arr;
int temp;
cin>>T;
for(int i=0;i<T;i++)
{
cin>>temp;
n.push_back(temp);
cin>>temp;
m.push_back(temp);
vector<int>temp_vec(temp);
for(int j=0;j<temp;j++)
{
int temp2;
cin>>temp2;
temp_vec[j]=(temp2);
}
sort(temp_vec.begin(),temp_vec.end());
arr.push_back(temp_vec);
cout<<endl;
}
return(0);
}

When you declare a vector of any type it does not have any elements until you initialize and put the value into it. Your code:
arr.push_back(temp_vec);
is trying to insert temp_vec into a non-existent vector inside the vector arr at position 0.
You should know the size of the vector that you want to use and then initialize that with the size of the constructor:
vector<vector<int> >arr(size);
for example:
vector<vector<int> >arr(64);
This will be initialize the vector arr with 64 or x empty elements.
Same as this

Related

Segmentation fault by pushing back 2D vector

I have a function that takes a numeric string and assigns each element into a single dimension vector:
void insert(vector<int> &matrix, string s)
{
for (int i=0; i<s.length(); i++)
matrix.push_back(s[i]-'0');
}
Then, at my main function I declare a 2D vector:
vector<vector<int>> matrix;
Now for example I try to execute the function:
insert(matrix[0],"12345");
It will simply give me error Segmentation fault, and I don't know why.
How do I solve this problem ?
I think because you are declaring your 2Dvector like
vector<vector> matrix;
so by declaring like this your matrix is of size 0.
Since your matrix is empty you cannot access elements from that. Instead init your 2d vector with some default values like 0 and give a try.
#include <vector>
#include <iostream>
using namespace std;
void insert(vector<int> &matrix, string s)
{
for (int i=0; i<s.length(); i++)
matrix.push_back(s[i]-'0');
}
int main()
{
int n=2,m=2;
vector<vector<int> > matrix(n, vector<int>(m));
insert(matrix[0],"12345");
for (auto i:matrix)
{
for (auto j:i)
{
cout << j;
}
}
}
I am not sure if this is what you are expecting from your insert func

Segmentation Fault (SIGSEGV) when getting data as input in the vector of struct

Segmentation Fault (SIGSEGV). Link to code
The code gives Segmentation fault error due to the input taken to store the values for vector of struct.
#include<bits/stdc++.h>
using namespace std;
struct s{
int a;
int d;
};
int main(){
int n;
cin>>n;
vector<s> v;
for(int i=0;i<n;i++){
cin>>v[i].a;
}
for(int i=0;i<n;i++){
cin>>v[i].d;
}
return 0;
}
Input is:
6
900 940 950 1100 1500 1800
910 1200 1120 1130 1900 2000
The problem is that you're accessing the vector outside of its bounds. The vector is empty, but v[i] tries to access elements of the vector that don't exist. Therefore the behaviour of the program is undefined.
I suspect that you may have intended to use the vector's constructor that takes a count of elements as an argument.
Here is the code that works. I specify the size of the vector when I construct it.
vector<s> v(n);
Compile code:
#include<bits/stdc++.h>
using namespace std;
struct s{
int a;
int d;
};
int main(){
int n;
cin>>n;
vector<s> v(n);
for(int i=0;i<n;i++){
cin>>v[i].a;
}
for(int i=0;i<n;i++){
cin>>v[i].d;
}
return 0;
}

index out of bound exception while accessing vector elements

#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{ vector <int> a,b;
int i,n,m,k;
bool cond=true;
cin>>n;
for(i=0;i<n;i++)
{
cin>>m;
a.push_back(m);
}
for(i=1;i<n-1;i++)
{
if(a.at(i)==a.at(i+1))
a.erase(a.begin()+i);
}
n=a.size();
for(i=0;i<n;i++)
cout<<a[i];
}
return 0;
}
I want to delete all repetitions of consecutive numbers.
This program throws an "out of bounds" error.
Is there any logical error, or could you suggest some better algorithm?
You are accessing out of bounds since after erasing an element your container is now one element shorter than the number of times you are iterating.
In any case; you can solve your problem in two lines if you just use what's already available in the standard library:
auto last = std::unique(a.begin(), a.end());
a.erase(last, a.end());
See also: http://en.cppreference.com/w/cpp/algorithm/unique

How do I sort a vector<int> which is an element of a 2-d matrix?

I am trying to use STl sort on a vector which is a part of a 2d array, but getting an error. What is the correct way to do this? Trying to use vector normally as sort(A,A+A.size()) where A is the vector.
I have added a comment at the sort statement that is giving an error.
#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
while(1)
{
int m,n;
cin>>m>>n;
int arr[m][n];
for(int x=0;x<m;x++)
for(int y=0;y<n;y++)
cin>>arr[x][y];
vector<int> cost[m][n]; //THIS IS THE 2-D ARRAY OF VECTORS
cost[0][0].push_back(arr[0][0]);
for(int i=1;i<m;i++)
{
cost[0][i].push_back(cost[0][i-1][0]+arr[0][i]);
}
for(int i=1;i<n;i++)
{
cost[i][0].push_back(cost[i-1][0][0]+arr[i][0]);
}
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
vector<int>::iterator it;
for(it=cost[i-1][j].begin(); it!=cost[i-1][j].end();it++)
cost[i][j].push_back(((*it)+arr[i][j]));
vector<int>::iterator it1;
for(it1=cost[i][j-1].begin(); it1!=cost[i][j-1].end();it1++)
cost[i][j].push_back(((*it1)+arr[i][j]));
//freevec(cost[i][j]);
}
}
int tx,ty,k;
cin>>tx>>ty>>k;
//THIS STEP IS GIVING AN ERROR.
sort(cost[tx][ty],cost[tx][ty]+cost[tx][ty].size());
//THIS STEP IS GIVING AN ERROR.
cout<<cost[tx][ty][k-1]<<endl;
/*
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{ cout<<"\n\n"<<arr[i][j]<<endl;
vector<int>::iterator it;
for(it=cost[i][j].begin(); it!=cost[i][j].end();it++)
cout<<(*it)<<" ";
}
cout<<endl;
}*/
}
}
A vector doesn't change its properties by being an element of an array. So you would sort it just like you would sort a vector that is not an element of a 2D array:
sort(cost[tx][ty].begin(), cost[tx][ty].end());
or
sort(begin(cost[tx][ty]), end(cost[tx][ty]));
Assuming argument dependent lookup finds std::sort, std::begin and std::end.

How to fill a vector of struct

I have the following code, where i defined a vector of vector of struct
#include <vector>
#include <iostream>
using namespace std;
struct node
{
int index;
double value;
};
int main()
{
vector < vector <node> >vett1;
node p;
p.index=5;
p.value=2;
for (int i=0; i<10; i++)
vett1[i].push_back(p);
return 0;
}
i don't know the right way to fill it. In this way when i run it, compilers gives me segmentation fault error.
When you access vett1[i], but the vett1 has not been filled with size zero. That's why the segmentation fault error occur.
Three ways to fix it:
Add
vett1.resize(10);
before the for loop.
Or define vett1 and set its size as follows:
vector <vector <node>> vett1(10);
Or you can do this if you don't know the exact size pre-hand:
for (int i=0; i<10; i++)
{
vector<node> temp;
temp.push_back(p);
vett1.push_back(temp);
}