container-data-type

range based for loop with const shared_ptr<>

亡梦爱人 提交于 2019-12-23 08:38:09
问题 I have a container with shared_ptr<> , e.g. a vector<shared_ptr<string>> v and I'd like to iterate over v indicating const-ness. This code: vector<shared_ptr<string>> v; v.push_back(make_shared<std::string>("hallo")); ... for (const auto &s : v) { *s += "."; // <<== should be invalid } looks like what I want to do (indicating that s is const ) but of course it does not make the string const . Is there an elegant way to iterate over a container of shared_ptr which makes clear that the content

How to return a private pointer to a list of pointers as const?

孤者浪人 提交于 2019-12-12 10:05:04
问题 I have a pointer to a list of pointers, as a private variable. I also have a getter that returns the pointer to the list. I need to protect it from changes. I couldn't find how to use reinterpret_cast or const_cast on this. class typeA{ shared_ptr<list<shared_ptr<typeB>>> l; public: shared_ptr<list<shared_ptr<const typeB>>> getList(){return (l);}; }; The compiler returns: error: could not convert ‘((typeA*)this)->typeA::x’ from ‘std::shared_ptr<std::__cxx11::list<std::shared_ptr<typeB> > >’

Why no front() method on std::map (and other associative containers from the STL)?

老子叫甜甜 提交于 2019-12-10 12:31:43
问题 The STL reference seems to make a conceptual difference between : 'Sequence containers' (array vector deque forward_list list) on one hand 'Associative containers' (set multiset map multimap unordered_set unordered_multiset unordered_map unordered_multimap) on the other hand. Also, it seems like we have : all containers implementing a begin() method returning an iterator pointing to the first element in the container. only the sequence containers having a front() method returning a reference

Const correctness causing problems with containers for pointers?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 11:33:13
问题 Given this code (C++, Qt containers are used but I suppose the question is universal): // a containter for Item-s QList<Item*> items; // argument is const to prevent changing the item by this function void doStuff(const Item *item) { // find index of the item inside the container // indexOf() is declared as: // template <typename T> int QList<T>::indexOf(const T &t, int from = 0) const const int itemIndex = items->indexOf(item); } I get a compile error (MSVC2010): error C2664: 'QList::indexOf

Why use non-member begin and end functions in C++11?

情到浓时终转凉″ 提交于 2019-11-26 04:35:15
问题 Every standard container has a begin and end method for returning iterators for that container. However, C++11 has apparently introduced free functions called std::begin and std::end which call the begin and end member functions. So, instead of writing auto i = v.begin(); auto e = v.end(); you\'d write using std::begin; using std::end; auto i = begin(v); auto e = end(v); In his talk, Writing Modern C++, Herb Sutter says that you should always use the free functions now when you want the begin

&#39;size_t&#39; vs &#39;container::size_type&#39;

与世无争的帅哥 提交于 2019-11-26 02:06:30
问题 Is there is a difference between size_t and container::size_type ? What I understand is size_t is more generic and can be used for any size_type s. But is container::size_type optimized for specific kinds of containers? 回答1: The standard containers define size_type as a typedef to Allocator::size_type (Allocator is a template parameter), which for std::allocator<T>::size_type is typically defined to be size_t (or a compatible type). So for the standard case, they are the same. However, if you

In which scenario do I use a particular STL container?

风流意气都作罢 提交于 2019-11-25 23:58:24
问题 I\'ve been reading up on STL containers in my book on C++, specifically the section on the STL and its containers. Now I do understand each and every one of them have their own specific properties, and I\'m close to memorizing all of them... But what I do not yet grasp is in which scenario each of them is used. What is the explanation? Example code is much prefered. 回答1: This cheat sheet provides a pretty good summary of the different containers. See the flowchart at the bottom as a guide on

Why can&#39;t I make a vector of references?

只谈情不闲聊 提交于 2019-11-25 22:44:49
问题 When I do this: std::vector<int> hello; Everything works great. However, when I make it a vector of references instead: std::vector<int &> hello; I get horrible errors like error C2528: \'pointer\' : pointer to reference is illegal I want to put a bunch of references to structs into a vector, so that I don\'t have to meddle with pointers. Why is vector throwing a tantrum about this? Is my only option to use a vector of pointers instead? 回答1: The component type of containers like vectors must