stdvector

How to do the vector of sets in C++?

不羁岁月 提交于 2019-12-01 03:38:29
问题 I can do a simple array of sets: set < char > * words = new set < char > [10] How I can do a vector of sets? This results in a compiler error: vector < set< char >> v . Thank you for answers! 回答1: If vector < set< char >> v is exactly what you've got there (I hope you cut and pasted), you've run into one of the annoying little features of C++. Those >> look to you like two closing angle brackets for two templates. They look like a right shift operator to the compiler. Change them to > > with

vector<char> VS vector<bool> in C++11 [closed]

流过昼夜 提交于 2019-12-01 03:13:35
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Why should we use vector<char> instead of vector<bool> ? What is the reason that vector<char> is faster? 回答1: std::vector<bool> is a specialisation of std::vector<T> that's done mainly for space efficiency (debatable). However, it behaves similarly but not equally as a

Why does std::vector::insert invalidate all iterators after the insertion point

耗尽温柔 提交于 2019-12-01 02:07:08
问题 When insert -ing into a std::vector the C++ standard assures that all iterators before the insertion point remain valid as long as the capacity is not exhausted (see [23.2.4.3/1] or std::vector iterator invalidation). What is the rationale behind not allowing iterators after the insertion point to remain valid (if the capacity is not exhausted)? Of course, they would then point to a different element but (from the presumed implementation of std::vector ) it should still be possible to use

Why does an empty vector call the value type's default constructor?

余生颓废 提交于 2019-12-01 02:05:56
Using g++, I observe that creating a vector of size zero calls the vector's parameterized object type's constructor once. It then is deleted. Why does this happen? #include <iostream> #include <vector> using namespace std; class s { public: s() { cout << endl << "default s constructor" << endl; } ~s() { cout << endl << "default s destructor" << endl; } }; int main() { vector<s> v(0); } Output: default s constructor default s destructor Because you're explicitly passing an initial size, which calls a constructor that has another parameter whose default value is s() . Just leave out the (0) (i.e

Question about storing array in a std::vector in C++

家住魔仙堡 提交于 2019-12-01 01:59:13
问题 I am unclear about the following. First, this code compiles fine: #include <vector> typedef struct{ int x1,x2,x3,x4; } ints; typedef std::vector<ints> vec; int main(){ vec v; ints a = {0,1,2,3}; v.push_back(a); } The following code is near identical: #include <vector> typedef std::vector<int[4]> vec; int main(){ vec v; int a[4] = {0,1,2,3}; v.push_back(a); } but it throws the extremely length error output I will include at the end. Why does the compiler treat these two programs so differently

When should we use reserve() of vector?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 22:30:36
I always use resize() because I cannot use reserve as it gives error: vector subscript out of range. As I've read info about the differences of resize() and reserve(), I saw things like reserve() sets max. number of elements could be allocated but resize() is currently what we have. In my code I know max. number of elements but reserve() doesn't give me anything useful. So, how can I make use of reserve()? codeling A vector has a capacity (as returned by capacity() and a size (as returned by size() . The first states how many elements a vector can hold, the second how many he does currently

Why does an empty vector call the value type's default constructor?

我的梦境 提交于 2019-11-30 21:30:49
问题 Using g++, I observe that creating a vector of size zero calls the vector's parameterized object type's constructor once. It then is deleted. Why does this happen? #include <iostream> #include <vector> using namespace std; class s { public: s() { cout << endl << "default s constructor" << endl; } ~s() { cout << endl << "default s destructor" << endl; } }; int main() { vector<s> v(0); } Output: default s constructor default s destructor 回答1: Because you're explicitly passing an initial size,

std::remove_if using other class method

早过忘川 提交于 2019-11-30 19:18:55
问题 I want to use std::remove_if with a predicate that is a member function of a differenct calss. That is class B; class A { bool invalidB( const B& b ) const; // use members of class A to verify that B is invalid void someMethod() ; }; Now, implementing A::someMethod , I have void A::someMethod() { std::vector< B > vectorB; // filling it with elements // I want to remove_if from vectorB based on predicate A::invalidB std::remove_if( vectorB.begin(), vectorB.end(), invalidB ) } Is there a way to

push_back or emplace_back with std::make_unique

时间秒杀一切 提交于 2019-11-30 17:29:21
Based on the answers in these questions here , I know that it is certainly preferred to use c++14's std::make_unique than to emplace_back(new X) directly. That said, is it preferred to call my_vector.push_back(std::make_unique<Foo>("constructor", "args")); or my_vector.emplace_back(std::make_unique<Foo>("constructor", "args")); That is, should I use push_back or emplace_back when adding an std::unique_ptr constructed from std::make_unique ? ==== EDIT ==== and why? c: <-- (tiny smile) It doesn't make a difference as far as construction of the new object is concerned; you already have a unique

Why is std::vector contiguous?

蓝咒 提交于 2019-11-30 17:27:46
Besides the fact that the standard defines it to be contiguous, why is std::vector contiguous? If it runs out of space, it needs to reallocate a new block and copy the old block to the new one before continuing. What if it wasn't contiguous? When the storage fills up, it would just allocate a new block and keep the old block. When accessing through an iterator, it would do simple >, < checks to see which block the index is in and return it. This way it doesnt need to copy the array every time it runs out of space. Would this really work/be better? or am i missing something? If std::vector didn