stdvector

Vector constructor with two parameters is parsed as a function declaration

拟墨画扇 提交于 2019-11-28 04:43:45
问题 Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() { std::string sen = "abc def ghi jkl"; std::istringstream iss(sen); std::vector<std::string> // declaration in question vec(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()); std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, "\n")); } The compiler throws an error at the call to std::copy request for member 'begin' in 'vec',

Fastest way to reset every value of std::vector<int> to 0

谁说我不能喝 提交于 2019-11-28 02:45:49
What's the fastest way to reset every value of a std::vector<int> to 0 and keeping the vectors initial size ? A for loop with the [] operator ? Cat Plus Plus std::fill(v.begin(), v.end(), 0); As always when you ask about fastest: Measure! Using the Methods above (on a Mac using Clang): Method | executable size | Time Taken (in sec) | | -O0 | -O3 | -O0 | -O3 | ------------|---------|---------|-----------|----------| 1. memset | 17 kB | 8.6 kB | 0.125 | 0.124 | 2. fill | 19 kB | 8.6 kB | 13.4 | 0.124 | 3. manual | 19 kB | 8.6 kB | 14.5 | 0.124 | 4. assign | 24 kB | 9.0 kB | 1.9 | 0.591 | using

std::vector of std::vectors contiguity

好久不见. 提交于 2019-11-28 00:42:39
I know that std::vector<T> internally stores it's data contiguously (unless it is std::vector<bool> ) both in the old C++03 standard and the new C++11 . Nice stackoverflow questions that deal with this and quote the standard: answer , answer . What about the data inside nested vectors std::vector <std::vector <T> > ? How is that stored? If every internal vector needs to store it's data contiguously, how can it be true that &v[n] == &v[0] + n for all 0 <= n < v.size() . To phrase this slightly different, is it possible to access all the elements stored in such nested structure "simply" and

How to cheaply assign C-style array to std::vector?

折月煮酒 提交于 2019-11-27 21:56:53
Currently I do the following: // float *c_array = new float[1024]; void Foo::foo(float *c_array, size_t c_array_size) { //std::vector<float> cpp_array; cpp_array.assign(c_array, c_array + c_array_size); delete [] c_array; } How can I optimize this assigning? I would like not to perform elementwise copy but just swap pointers. The current std::vector doesn't provide any capability or interface to take ownership of previously allocated storage. Presumably it would be too easy to pass a stack address in by accident, allowing more problems than it solved. If you want to avoid copying into a vector

Initialize std::array with a range (pair of iterators)

风流意气都作罢 提交于 2019-11-27 21:19:42
How can I initialize an std::array from a range (as defined by a pair of iterators)? Something like this: vector<T> v; ... // I know v has exactly N elements (e.g. I just called v.resize(N)) // Now I want a initialized with those elements array<T, N> a(???); // what to put here? I thought array would have a constructor taking a pair of iterators, so that I could do array<T, N> a(v.begin(), v.end()) , but it appears to have no constructors at all! I know I can copy the vector into the array, but I'd rather initialize the array with the vector contents directly, without default-constructing it

Vector of std::function with different signatures

别等时光非礼了梦想. 提交于 2019-11-27 20:41:25
I have a number of callback functions with different signatures. Ideally, I would like to put these in a vector and call the appropriate one depending on certain conditions. e.g. void func1(const std::string& value); void func2(const std::string& value, int min, int max); const std::vector<std::function<void(std::string)>> functions { func1, func2, }; I realise the above isn't possible, but I wonder if there are any alternatives I should consider. I haven't been able to find any yet, and I've experimented with std::bind but not managed to achieve what I want. Is such a thing possible? You

Initialisation of static vector

夙愿已清 提交于 2019-11-27 20:38:50
问题 I wonder if there is the "nicer" way of initialising a static vector than below? class Foo { static std::vector<int> MyVector; Foo() { if (MyVector.empty()) { MyVector.push_back(4); MyVector.push_back(17); MyVector.push_back(20); } } } It's an example code :) The values in push_back() are declared independly; not in array or something. Edit: if it isn't possible, tell me that also :) 回答1: Typically, I have a class for constructing containers that I use (like this one from boost), such that

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

爷,独闯天下 提交于 2019-11-27 19:40:56
I'm having a std::vector with elements of some class ClassA . Additionally I want to create an index using a std::map<key,ClassA*> which maps some key value to pointers to elements contained in the vector. Is there any guarantee that these pointers remain valid (and point to the same object) when elements are added at the end of the vector (not inserted ). I.e, would the following code be correct: std::vector<ClassA> storage; std::map<int, ClassA*> map; for (int i=0; i<10000; ++i) { storage.push_back(ClassA()); map.insert(std::make_pair(storage.back().getKey(), &(storage.back())); } // map

Initialization of std::vector<unsigned int> with a list of consecutive unsigned integers

邮差的信 提交于 2019-11-27 16:39:49
问题 I want to use a special method to initialize a std::vector<unsigned int> which is described in a C++ book I use as a reference (the German book 'Der C++ Programmer' by Ulrich Breymann, in case that matters). In that book is a section on sequence types of the STL, referring in particular to list , vector and deque . In this section he writes that there are two special constructors of such sequence types, namely, if X refers to such a type, X(n, t) // creates a sequence with n copies of t X(i,

std::vector as a template function argument

坚强是说给别人听的谎言 提交于 2019-11-27 13:44:57
问题 I want to make a class method that takes a std::vector reference as an argument and I want to use it with different types of data. The function should look like: void some_function(const std::vector & vect){ //do something with vector } and I want use it with for example: std::vector<int> v1; some_function(v1); std::vector<string> v2; some_function(v2); I hope that I made my point clear. Do I have to make a template method like that: template<class T> void some_function(std::vector<T> & vect)