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);
}
Related
I am trying to fill an array with different integers, but it doesn't work as expected.
#include <iostream>
using namespace std;
int main(){
int i=0;
int num;
int MyArray[]={};
while (true) {
cout<<"sayi giriniz"<<endl;
cin>>num;
MyArray[i]=num;
i++;
for (int j=0; j<i; j++) {
cout<<MyArray[j]<<endl;
}
}
return 0;
}
https://imgur.com/a/tANGpSY
When I enter the 3rd value it gives an unexpected result.
int MyArray[]={};
Now, MyArray has a size of 0, so any indexes that you tried to access MyArray will cause undefined behavior.
If you want to make an array that is dynamically in size, use std::vector in the <vector> header.
Change
int MyArray[]={};
to
std::vector<int> MyArray;
This:
MyArray[i]=num;
i++;
To this:
MyArray.push_back(num); // you don't even need i
This
for (int j=0; j<i; j++) {
cout<<MyArray[j]<<endl;
}
To this:
for(const auto &i : MyArray) // range based for loop, recommend
{
std::cout << i << '\n';
}
Also, using namespace std; is bad, so don't use it.
If you want to take input and are unsure about the number of elements, you should use a vector. The array which you have made is of 0 size. It will surely give you an error.
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
I tried making a data structure in C++ by STL vector, however, I got a EXC_BAD_ACCESS error at // error here after running the program below (on Xcode). Is there any solution?
The data structure nodes is a vector, one of which member is also a vector costs.
#include <vector>
using namespace std;
struct t_node{
int id;
vector<int> costs;
};
int main(){
vector<t_node> nodes;
for(int i=0; i<3; i++){
t_node input_node;
input_node.id = i;
for(int j=0; j<5; j++){
input_node.costs.push_back(j);
}
nodes.push_back(move(input_node)); // error here, "EXC_BAD_ACCESS"
}
return 0;
}
It could be because of input_node is temporal...?
(Thanks for the comments, I updated the code)
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
Writing code for the matrix rotation question in hackerrank. i am getting segmentation fault. the code is not complete. i have commented on the statements which i think is creating the prblem but cant identify the mistake . plz help.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct coord
{
int i,j;
} starting, current;
struct reference
{
float i,j;
} center;
int **a,m,n,t1,t2;
int goup()
{
float dist,dist2;
dist=sqrt((center.i-current.i)*(center.i-current.i)+(center.j-current.j)*(center.j-current.j));
do
{
t1=a[current.i-1][current.j];
a[current.i-1][current.j]=t2;//segmentation fault at this statement
t2=t1;
current.i--;
dist=sqrt((center.i-current.i)*(center.i-current.i)+(center.j-current.j)*(center.j-current.j));
} while(dist2!=dist);
return 0;
}
int main()
{
int t;
cin>>m>>n>>t;
a=new int*[m];
for(int i=0;i<m;i++)
a[i]=new int[n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
center.i=(float)(m-1)/2.0;
center.j=(float)(n-1)/2.0;
starting.i = center.i + (0.5)*(m%2+1);
starting.j = center.j + (0.5)*(n%2+1);
current.i=starting.i;
current.j=starting.j;
t2=a[starting.i][starting.j];
t1=a[starting.i-1][starting.j];
while(current.i!=m)
{
for(int i=0;i<t;i++)
{
goup();
// goleft();
// godowm();
// goright();
}
starting.i++;
starting.j++;
current.i=starting.i;
current.j=starting.j;
}
return 0;
}
Since dist2 is never initialised, dist2!=dist is rarely true and you decrement current.i so many times that you wind up accessing outside the array and dereferencing whatever random number might be lying around there.
It's impossible to say how to fix it since you haven't left any clue about what the function is supposed to accomplish.
Initialising dist2 with whatever value you want it to have would be a good start, but the condition is still unlikely to ever be true.