whats wrong with this simple program. I want to create multi list and insert using c++ STL. its giving segmentation fault.
#include <iostream>
#include <list>
using namespace std;
int main(){
list<int> *l;
l[0].push_back(1);
l[10].push_back(12);
cout<<endl;
return 0;
}
Why are you using a pointer to a list? You didn't allocate memory for the list. You could use a container to store multiple lists, e.g. std:array for static number of elements or std::vector for dynamic number of elements:
#include <array>
#include <iostream>
#include <list>
#include <vector>
using std::array;
using std::vector;
using std::list;
using std::cout;
int main(){
std::array<list<int>, 11> l;
l[0].push_back(1);
l[10].push_back(12);
std::vector<list<int>> l2(11);
l2[0].push_back(1);
l2[10].push_back(12);
cout << '\n';
return 0;
}
list<int> *l; makes l a pointer to a list<int> but it doesn't actually create a list<int> - and definitely not an array of list<int> which you are trying to access.
Possible solutions.
Plain C fixed size arrays of list<int>:
#include <iostream>
#include <list>
int main() {
std::list<int> l[11]; // place for 11 list<int>`'s
l[0].push_back(1);
l[10].push_back(12);
}
Using the C++ fixed sized std::array:
#include <array>
#include <list>
int main() {
std::array<std::list<int>, 11> l; // place for 11 list<int>'s
l[0].push_back(1);
l[10].push_back(12);
}
Using a C++ std::vector that allows for dynamicially adding more list<int>'s:
#include <list>
#include <vector>
int main() {
std::vector<std::list<int>> l(11); // starts with place for 11 list<int>'s
l[0].push_back(1);
l[10].push_back(12);
}
In list<int> *l; is creating a pointer to a list. Before accessing l you need to assign valid address to it.
Something like this,
list<int> l;
list<int> *l2 = &l;
list<int> *l; is a pointer to list, it is not initialized, so adding elements to it has undefined behaviour. Either you initialize it to a variable like:
list<int> l;
l.push_back(1);
l.push_back(12);
In this case you can only access the elements that already have data, l[0] or l[1].
Or you need to allocate space to the number of elements you need in your list.
For instance:
list<int> l[20];
l[0].push_back(1);
l[10].push_back(12);
Related
I'm trying to take an even integer array and odd integer list, and then try to merge them in a vector, using the merge algorithm available in STL of C++. The task requires specifically to use the merge algorithm, and I'm getting stuck in error at line 43 , right of which I've commented that that's the error point.
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstring>
#include <vector>
#include <list>
#include <cstdlib>
using namespace std;
void display(list<int>&l){
list<int>::iterator p;
for(p=l.begin();p!=l.end();++p){
cout<<*p<<", ";
}
cout<<"\n";
}
void display(int arr[6]){
for(int i=0;i<6;i++){
cout<<arr[i]<<" ";
}
cout<<"\n";
}
int main()
{
int inp;
int even_arr[6];
list<int> odd_list(6);
cout<<"Enter even numbers: ";
for(int i=0;i<6;i++){
cin>>even_arr[i];
}
display(even_arr);
list<int>::iterator p;
cout<<"\nEnter odd numbers: ";
for(p=odd_list.begin();p!=odd_list.end();++p){
cin>>inp;
*p = inp;
}
display(odd_list);
vector<int> vec1(12);
list<int>::iterator itr=odd_list.begin();
int *ptr=even_arr;
merge(even_arr,even_arr+6,odd_list,odd_list+6,vec1); //ERROR LINE
return 0;
}
You can't add something to a std::list like you can do with arrays. Also it's better to be explicit about iterators and use std::end to get the past-the-end-iterator, since you want to use the whole array anyway.
Also std::merge() expects iterators but the last parameter is vec1, which is a std::vector. The correct version would be:
merge(begin(even_arr), end(even_arr),
begin(odd_list), end(odd_list),
begin(vec1));
I omitted std:: to improve readability.
Use of std::begin over odd_list.begin() (and similar) is debatable and up to personal preference.
I am trying to convert an array to a vector pointer. I tried using std::copy, however, this produced a SegFault error. Is there any way to do this?
Thank you so much and I really appreciate it.
#include <iostream>
#include <iterator>
#include <algorithm>
int main () {
int myints[]={10,20,30,40,50,60,70};
std::vector<int> *myvector (7);
std::copy ( myints, myints+7, *myvector->begin() );
}
I'm not entirely convinced that you actually want a pointer to a vector, since it seems unlikely that that would be the case (also std::vector<int> *myvector(7); doesn't instantiate a vector it creates a pointer to std::vector. If you want to instantiate a vector, this works:
std::vector<int> myVector(myInts, myInts + (sizeof(myInts)/sizeof(*myInts)));
Otherwise,
pointer:
auto myVector = new std::vector<int>(myInts, myInts + (sizeof(myInts)/sizeof(*myInts)));
smartPointer:
auto myVector = std::make_unique<std::vector<int>>(myInts, myInts + (sizeof(myInts)/sizeof(*myInts)));
#include <iostream>
#include <iterator>
#include <algorithm>
#include <memory>
int main () {
int myints[]={10,20,30,40,50,60,70};
auto myvector = std::make_unique<std::vector<int>>(7);
std::copy ( myints, myints+7, myvector->begin() );
}
To do this conversion to a regular vector would be. Using a vector pointer is not a good idea because it can cause unwanted behavior. Also your vector pointer was never actually constructed.
#include <iostream>
#include <iterator>
#include <algorithm>
int main () {
int myints[]={10,20,30,40,50,60,70};
std::vector<int> myvector (7);
std::copy ( myints, myint+7, myvector.begin() );
}
the following codes pushed back an std::array to a std::vector N times. Is there a more elegant and shorter way of doing this?
#include <iostream>
#include <vector>
#include <array>
#include <iomanip>
#include <complex>
#include <cmath>
int main () {
int N=10;
std::vector< std::array<std::complex<double>,3> > v;
v.reserve(N);
for(int i=0;i<N;i++){
std::array<std::complex<double>,3> el { {0.0,3.0,0.0} };
v.push_back(el);
}
}
Yes but you have to use parentheses when constructing vector
std::vector< std::array<std::complex<double>,3> > v(n, {0.0,3.0,0.0});
If braces are used than initialization list is preferred and in this case you could have unexpected errors.
You can use the std::vector::insert (#3 in the overload set) member function:
int N=10;
std::vector< std::array<std::complex<double>,3> > v;
v.reserve(N);
v.insert(v.end(), N, { {0.0,3.0,0.0} });
Note that #MarekR's answer is preferable for initializing the vector, as it circumvents the call to reserve, and setting up an object during initialization is usually better than subsequent member function calls. The above call to std::vector::insert instead is suitable for adding additional elements later on.
I'm created a pointer to a vector and adding a member to it but I can't call that member by index without getting a seg fault. Why is that?
When I use the back() method I can access it fine.
Example using index:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> * a;
a->push_back(2);
cout<< (*a)[0] << endl;
}
// Segmentation fault (core dumped)
Example using back():
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> * a;
a->push_back(2);
cout<< (*a).back() << endl;
}
// 2
Does this mean that the back of my vector is not at index = 0 ?
The example using back() is wrong. You should new a vector object before using it:
a = new vector<int>();
Yes, your code compiles. Different compilers have different initial values of a. On my system, a is NULL, so it will throw out a "segfault" on a->push_back(2).
Try different compilers, the result will be different.
I am not sure what will happen if a has an uninitialized value, but it is dangerous.
I'm trying to find a solution for this problem
http://codeforces.com/problemset/problem/159/A
To find the solution, I thought of presenting the input by making a map of map.
And for this input my map should appear like this:
m["vasya"]={{"petya",1}}
m["petya"]={{"vasya",2}}
m["anya"]={{"ivan",2}}
m["ivan"]={{"anya",4}}
Below is my code.
and my assignment code:
// m[s1]=(ii(s2,t));
is wrong
my question is how can i assign to the map of map?
#include<iostream>
#include <cstdio>
#include <map>
#include <set>
#include <string>
#include<vector>
using namespace std;
int main(){
typedef map<string, int> ii;
map<string, ii> m;
int n,d;
cin>>n>>d;
string s1,s2,t;
for(int i=0;i<n;i++)
{
cin>>s1>>s2>>t;
// m[s1]=(ii(s2,t));
}
return 0;
}
Could anyone help?
You can write it like this:
m[s1][s2] = t;
I'm assuming you meant int t; rather than string t; since the last type in your map is int.
You want to make a more descriptive typedef than ii, but specifically you just want to add a new key-value pair into the map:
m[s1].insert(std::make_pair(s2, t));
Or if you want to split it up for clarity:
ii& submap = m[s1];
submap[s2] = t;
Or, if C++11 is available to you, exactly what you expressed in your question:
m[s1] = {{s2, t}};