stdvector

Convert vector<int> to integer

那年仲夏 提交于 2019-12-06 05:30:25
问题 I was looking for pre-defined function for converting a vector of integers into a normal integer but i din't find one. vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); Need this: int i=123 //directly converted from vector to int Is there a possible way to achieve this? 回答1: Using C++ 11: reverse(v.begin(), v.end()); int decimal = 1; int total = 0; for (auto& it : v) { total += it * decimal; decimal *= 10; } EDIT: Now it should be the right way. EDIT 2: See DAle's answer for a

initialize vector of pointers (automatically)

▼魔方 西西 提交于 2019-12-06 05:26:12
I encountered a compilation error when trying to initialize a vector of pointers to NULL by way of the std::vector constructor. I simplify the instruction to keep it simple: vector<int*> v (100,NULL) I guess it has something to do with an incompatibility between const T& value= T() (the parameter of the constructor) and the very value NULL, but I would appreciate some further explanation. Thank you If you have the relevant C++11 support, you could use nullptr : std::vector<int*> v(100, nullptr); However, in your particular case, there is no need to specify a default value, so the following

fast way to delete entries of STL vector of pointers

你离开我真会死。 提交于 2019-12-06 03:31:14
I have a vector of pointers which I want to delete, but iterating over the vector and calling delete for each element is quite slow. Is there a faster way? Unfortunately I really need to store pointers, since I use a virtual superclass. Simplified, the class structure looks something like this: class VirtualSuperClass { protected: SomeType m_someMember; // ... public: virtual void doSomething() = 0; }; class Subclass_1 : public VirtualSuperClass { protected: SomeType m_someSubclassMember; // ... public: virtual void doSomething() { /* do something*/ } }; class Subclass_2 : public

Insert std::map into std::vector directly

送分小仙女□ 提交于 2019-12-06 02:18:13
Sorry if the question is very trivial. I have a vector of maps: typedef map<char, int> edges; typedef vector<edges> nodes; nodes n; Now let's say I want to push a toy edge. I tried different things and what I worked is edges e; //declare an edge e['c'] = 1; //initialize it n.push_back(e); //push it to the vector How can I just push the pair of values of an edge ('c' and 2) without having to declare a variable and initialize it? Something like: n.push_back(edges('c',2)); but compiler gives an error error: no matching function for call to ‘std::map<char, int>::map(char, int)’ You can list

std::vector<Foo> when some members of Foo are references

陌路散爱 提交于 2019-12-06 00:34:15
问题 I often prefer to use references than pointers whenever possible, it makes the syntax cleaner in my opinion. In this case, I have a class: class Foo { public: Foo(Bar & bar) : bar_(bar) {} private: Bar & bar_; }; operator=() is implicitely deleted by the compiler for such a class, since once a reference is set, it cannot be changed (I can technically define my own that doesn't change bar_, but this would not be the required behaviour, so I'd rather the compiler complain if I try to assign a

Safe to use vector.emplace_back( new MyPointer ); Could failure inside vector lead to leaked memory?

有些话、适合烂在心里 提交于 2019-12-05 20:07:58
问题 Is it safe to use vector.emplace_back( new MyPointer() ); Or could an exception thrown or some failure inside vector cause memory leak? Would it be better to do some form of the following, where you put the pointer in a temporary unique_ptr first. vector.emplace_back( std::unique_ptr<MyPointer>( new MyPointer() ) ); So if a vector failure occurs the temporary unique_ptr will still clean up the memory? 回答1: It is not safe and would create a memory leak if you use the first version. The

std::vector alternative for C [closed]

本小妞迷上赌 提交于 2019-12-05 14:45:48
问题 Closed. This question is off-topic. It is not currently accepting answers. 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. 回答1: 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

vector iterators incompatible

此生再无相见时 提交于 2019-12-05 12:11:14
I'm currently working on a graph library for C++ and now got stuck at a point where I get an assertion error in debug mode during runtime. I also had a look an some other question here on SO but none of the questions and answers lead me to a solution. After reading in some forums I have the impression that this error happens because iterators become invalid as soon as the vector content is changed. (for example when using erase() ) But as you can see in my code, I'm not modifying the vector, just iterating. The error is in the line I marked with //ASSERTION . The strange thing is that neighbor

What is the most efficient way of copying elements that occur only once in a std vector?

一曲冷凌霜 提交于 2019-12-05 11:45:19
问题 I have a std vector with elements like this: [0 , 1 , 2 , 0 , 2 , 1 , 0 , 0 , 188 , 220 , 0 , 1 , 2 ] What is the most efficient way to find and copy the elements that occur only once in this vector, excluding the brute force O(n^2) algorithm? In this case the new list should contain [188, 220] 回答1: Make an unordered_map<DataType, Count> count; Iterate over the input vector increasing count of each value. Sort of count[value]++; Iterate over the count map copying keys for which value is 1. It

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

こ雲淡風輕ζ 提交于 2019-12-05 11:28:59
问题 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