stdvector

Initializing a 2D vector using initialization list in C++11

 ̄綄美尐妖づ 提交于 2019-12-04 03:29:14
How can i initialize a 2D vector using an initialization list? for a normal vector doing : vector<int> myvect {1,2,3,4}; would suffice. But for a 2D one doing : vector<vector<int>> myvect{ {10,20,30,40}, {50,60,70,80} }; What is a correct way of doing it? And how can i iterate through it using for? for(auto x: myvect) { cout<<x[j++]<<endl; } this for only shows: 10,1 ! And by the way what does this mean ? vector<int> myvect[5] {1,2,3,4}; i saw it here and cant understand it! Link What is a correct way of doing it? The way you showed is a possible way. You could also use: vector<vector<int>>

Passing std::vector<int> items to variadic function

拈花ヽ惹草 提交于 2019-12-04 03:13:45
问题 I'm using gcc 4.6. Assume that there is a vector v of parameters I have to pass to a variadic function f(const char* format, ...). One approach of doing this is: void VectorToVarArgs(vector<int> &v) { switch(v.size()) { case 1: f("%i", v[0]); case 2: f("%i %i", v[0], v[1]); case 3: f("%i %i %i", v[0], v[1], v[2]); case 4: f("%i %i %i %i", v[0], v[1], v[2], v[3]); // etc... default: break; } } // where function f is void f(const char* format, ...) { va_list args; va_start (args, format);

std::vector::reserve performance penalty

跟風遠走 提交于 2019-12-04 01:21:01
问题 inline void add(const DataStruct& rhs) { using namespace boost::assign; vec.reserve(vec.size() + 3); vec += rhs.a, rhs.b, rhs.c; } The above function was executed for about 17000 times and it performed (as far as I can see. There was some transformation involved) about 2 magnitudes worse with the call to vector::reserve. I always was under the impression that reserve can speed up push_back even for small values but this doesn't seem true and I can't find any obvious reasons why it shouldn't

std::vector alternative for C [closed]

拈花ヽ惹草 提交于 2019-12-04 00:58:29
Closed. This question is off-topic. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I wonder if there is an alternative for the std::vector in C? I found this implementation but it seems to contain some issues with memory reallocation. While reading C Array vs. C++ Vector , I found an interesting implementation of a simple vector container in C , which also includes push/pop operations. It's worth reading it! Pablo Santa Cruz You can give glib and its arrays ( GArray ) a try. glib

'std::vector<T>::iterator it;' doesn't compile

心已入冬 提交于 2019-12-03 23:04:29
I've got this function: template<typename T> void Inventory::insertItem(std::vector<T>& v, const T& x) { std::vector<T>::iterator it; // doesn't compile for(it=v.begin(); it<v.end(); ++it) { if(x <= *it) // if the insertee is alphabetically less than this index { v.insert(it, x); } } } and g++ gives these errors: src/Item.hpp: In member function ‘void yarl::item::Inventory::insertItem(std::vector<T, std::allocator<_CharT> >&, const T&)’: src/Item.hpp:186: error: expected ‘;’ before ‘it’ src/Item.hpp:187: error: ‘it’ was not declared in this scope it must be something simple, but after ten

Is the capacity required to be preserved when moving a std::vector?

a 夏天 提交于 2019-12-03 22:39:23
Consider the following code: std::vector vec; vec.reserve(500); size_t cap = vec.capacity(); std::vector newVec = std::move(vec); assert(cap == newVec.capacity()); In pretty much any implementation you run across, this will work. I don't care about what implementations do. I want to know what the standard requires . Will the moved-to vector have the same capacity as the original? Or will the assert trigger? Looking at the standard, it appears that nothing is required from the move constructor, however as @amaurea says, it would completely defeat the purpose of move semantics if the move

Modifying element of const std::vector<T> via const_cast

隐身守侯 提交于 2019-12-03 16:23:52
问题 Does the following program have undefined behavior? #include <iostream> #include <vector> struct Foo { const std::vector<int> x; }; int main() { std::vector<int> v = {1,2,3}; auto f = new Foo{v}; const_cast<int&>(f->x[1]) = 42; // Legal? std::cout << f->x[1] << "\n"; } Note that it not using const_cast to strip constness from f->x , but instead stripping constness from f->x[x] , which presumably is represented by a separate array. Or is a translation allowed to presume that f->x[1] is

How do I convert an armadillo matrix to a vector of vectors?

天大地大妈咪最大 提交于 2019-12-03 16:23:08
问题 I created an armadillo c++ matrix as follows: arma::mat A; A.zeros(3,4); I want to convert it to a vector of vectors defined by std::vector< std::vector<double> > B(3, std::vector<double>(4) ); How do I set B to equal A? If there is not an easy way for a vector of vectors, what about an array of arrays, i.e., what if I defined B to be double B[3][4]; 回答1: In such cases you should use arma::conv_to which is a totally superb feature of arma. Note that this method will require from a source

Best way to split a vector into two smaller arrays?

三世轮回 提交于 2019-12-03 14:48:32
问题 What I'm trying to do: I am trying to split a vector into two separate arrays. The current int vector contains an element per line in a text file. The text file is a list of random integers. How I'm planning to do it: My current idea is to create two regular int arrays, then iterate over the entire vector and copy n/2 elements to each of the arrays. What I would like to know: What is the most elegant way of accomplishing my task? I have a feeling that I can do this without iterating over the

How to convert std::queue to std::vector

耗尽温柔 提交于 2019-12-03 14:24:11
I need to make use of a queue of doubles because of the good properties it has as an ordered container. I want to pass this queue to a class constructor that accepts vectors. If I do that directly I get the following error: candidate constructor not viable: no known conversion from 'std::queue' to 'std::vector &' for 2nd argument How to cast a queue to a vector? The correct container to model both queue_like behaviour and vector-like behaviour is a std::deque . This has the advantages of: constant-time insertion and deletion at either end of the deque ability to iterate elements without