Vector of vectors, reserve

Since your inner dimension is constant, I think you want std::vector< std::array<int, 5> > vecs; vecs.reserve(N); This will give you preallocated contiguous storage, which is optimal for performance.

Best way to split a vector into two smaller arrays?

Use iterators. std::vector<int> lines; // fill std::size_t const half_size = lines.size() / 2; std::vector<int> split_lo(lines.begin(), lines.begin() + half_size); std::vector<int> split_hi(lines.begin() + half_size, lines.end()); Since iterator ranges represent half open ranges [begin, end), you don’t need to add 1 to the second begin iterator: lines.begin() + half_size isn’t copied to the first vector. Note that things … Read more

Pointers to elements of std::vector and std::list

Vectors – No. Because the capacity of vectors never shrinks, it is guaranteed that references, pointers, and iterators remain valid even when elements are deleted or changed, provided they refer to a position before the manipulated elements. However, insertions may invalidate references, pointers, and iterators. Lists – Yes, inserting and deleting elements does not invalidate … Read more

push_back or emplace_back with std::make_unique

It doesn’t make a difference as far as construction of the new object is concerned; you already have a unique_ptr<Foo> prvalue (the result of the call to make_unique) so both push_back and emplace_back will call the unique_ptr move constructor when constructing the element to be appended to the vector. If your use case involves accessing … Read more

std::vector to char* array

You can use std::transform as: std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert); Which requires you to implement convert() as: char *convert(const std::string & s) { char *pc = new char[s.size()+1]; std::strcpy(pc, s.c_str()); return pc; } Test code: int main() { std::vector<std::string> vs; vs.push_back(“std::string”); vs.push_back(“std::vector<std::string>”); vs.push_back(“char*”); vs.push_back(“std::vector<char*>”); std::vector<char*> vc; std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert); for ( size_t i = 0 … Read more

Setup std::vector in class constructor

Just do: MyClass::MyClass(int m_size) : size(m_size), vec(m_size, 0) You already seem to know about initializer lists, why not initialize vector there directly? vec = new vector<int>(size,0); is illegal because new returns a pointer and in your case vec is an object. Your second option: vector<int> temp(size,0); vec = temp; although it compiles, does extra work … Read more

Vector storage in C++

If you define your Point as having contiguous data storage (e.g. struct Point { int a; int b; int c; } or using std::array), then std::vector<Point> will store the Points in contiguous memory locations, so your memory layout will be: p0.a, p0.b, p0.c, p1.a, p1.b, p1.c, …, p(N-1).a, p(N-1).b, p(N-1).c On the other hand, if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)