How to Convert Arrays to Vector Pointers in C++? - c++

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

Related

using std::accumulate when there is pointer

Is this the only solution when there is a pointer that points to a vector and we would like to use accumulate to sum up numbers?
Is there any simpler solution rather than writing a lambda function and using a four argument type of accumulating?
Also, for using std::sort, will the situation be the same?
Here is the code:
#include <random>
#include <vector>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
const int N=3;
auto p=make_unique<array<int,N>> ();
(*p)[0]=3;
(*p)[1]=4;
(*p)[2]=5;
sum=accumulate(p,?,0);
return 0;
}
To answer your immediate question:
std::accumulate(p->begin(), p->end(), 0);
The same syntax will work for other STL algorithms as well.
Other improvements to your code snippet:
Avoid using #include<bits/stdc++.h>, see this post. Similarly for using namespace std, it's considered bad practise.
const N=3 -> const auto N=3
std::array is not a vector and you can initialise it directly using initializer-list syntax:
const auto* obj = new std::array<int,3>{3,4,5};

multi dimensional list c++ STL list

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

Pushing a std::array to a std::stack

I'm doing a problem where I need to create a stack of n-dimensional coordinates. I have implemented the coordinates as type std::array<std::size_t, n_dims>, where n_dims is a compile-time constant.
Q: What is the best way to push a coordinate to the stack?
Possibilities:
Creating the array as a variable, then pushing it to the stack. This seems wasteful. A bit less bad if I use move semantics:
std::array<std::size_t, 2> my_array = {1, 3};
my_stack.push(std::move(my_array));
// or my_stack.emplace(...)
but still unnecessarily complicated.
Using std::stack::emplace. This doesn't seem to work. I think it's because std::array is an aggregate type. (I'd like to understand this point better -- I believe one important consequence is that aggregate types have only default and copy constructors.)
#include <array>
#include <stack>
int main()
{
std::stack<std::array<int, 2>> st;
st.emplace(1,3); // doesn't work
st.emplace({1,3}); // doesn't work
st.emplace({{1,3}}); // doesn't work
}
What should I do?
You can use std::experimental::make_array
#include <experimental/array>
#include <array>
#include <stack>
int main()
{
std::stack<std::array<int, 2>> st;
st.emplace(std::experimental::make_array(1,3));
}

Elegant way to push back std::array to std::vector N times

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.

Wrap raw data in std container like array, with runtime size

Is there any std container which would be fixed size like std::array, but the size would not be compile time, but runtime?
I want to pass a part of some data I have stored in std::array to std::acculumate and similar functions. I do not want to use std::vector (working on embedded platform), therefore I am looking for something in between.
Assume code like this, what I want is something to be used in place of array_part:
#include <array>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
int main()
{
std::array<float,100> someData;
// fill the data
int dataCount = 50;
std::array_part<float> partOfData(someData.data(),dataCount)); // <<<<< here
const auto s_x = std::accumulate(partOfData.begin(), partOfData.end(), 0.0);
}
If there is no such container, how can I wrap the raw data I have and present them to std::accumulate and other std algorithms?
std::accumulate takes iterators. You can pass it iterators that contain the range of interest:
auto start = partOfData.begin() + 42;
auto end = partOfData.begin() + 77;
const auto s_x = std::accumulate(start, end, 0.0);
Alternatively, you can roll out your own non-owning container-like object. See this question for an example.