stl

Problem using templated predicate in STL algorithms

懵懂的女人 提交于 2020-01-01 11:59:26
问题 I have the following piece of code, that gives an error on the first usage form of pred2. I was hoping if someone could explain why this particular usage is incorrect, as I think pred3 usage is similar. #include <algorithm> bool pred1(const int&) { return true; } template<typename T> bool pred2(const T&) { return true; } struct pred3 { template<typename T> bool operator()(T&) { return true; } }; int main() { int A[] = { 2, 0, 4, 6, 0, 3, 1, -7 }; const int N = sizeof(A) / sizeof(int); std:

Vector iterator not dereferencable?

只谈情不闲聊 提交于 2020-01-01 11:36:17
问题 I'm getting that error with this code: for(std::vector<AguiTimedEvent*>::iterator it = timedEvents.begin(); it != timedEvents.end();) { if((*it)->expired()) { (*it)->timedEventCallback(); delete (*it); it = timedEvents.erase(it); } else it++; } What could be the problem? the timed event sometimes pushes a new one in when its callback is called, that might do it Thanks 回答1: If you are looping through a vector and the callback function causes the vector to be added to, then all iterators into

Vector iterator not dereferencable?

别来无恙 提交于 2020-01-01 11:34:07
问题 I'm getting that error with this code: for(std::vector<AguiTimedEvent*>::iterator it = timedEvents.begin(); it != timedEvents.end();) { if((*it)->expired()) { (*it)->timedEventCallback(); delete (*it); it = timedEvents.erase(it); } else it++; } What could be the problem? the timed event sometimes pushes a new one in when its callback is called, that might do it Thanks 回答1: If you are looping through a vector and the callback function causes the vector to be added to, then all iterators into

std::vector<std::string> crash

孤街醉人 提交于 2020-01-01 10:48:45
问题 This question is continuation of my question. Here is problematic code. a.h: #include <string> #include <vector> std::vector<std::string> foo(); a.cpp #include "a.h" std::vector<std::string> foo() { std::vector<std::string> v; return v; } and finally main.cpp: #include "a.h" #include <iostream> int main() { std::vector<std::string> s = foo(); return 0; } Compilation as following (main.cpp is compiled with STL debugging flag): g++ -c a.cpp g++ -D_GLIBCXX_DEBUG main.cpp a.o When running a.out,

Can I Have Polymorphic Containers With Value Semantics in C++11?

走远了吗. 提交于 2020-01-01 10:15:46
问题 This is a sequel to a related post which asked the eternal question: Can I have polymorphic containers with value semantics in C++? The question was asked slightly incorrectly. It should have been more like: Can I have STL containers of a base type stored by-value in which the elements exhibit polymorphic behavior? If you are asking the question in terms of C++, the answer is "no." At some point, you will slice objects stored by-value. Now I ask the question again, but strictly in terms of C+

What is the concatenation complexity of balanced ropes?

眉间皱痕 提交于 2020-01-01 10:14:14
问题 I've looked at different papers and here is the information that I've gathered: SGI implementation and C cords neither guarantee O(1) time concatenation for long ropes nor ~log N depth for shorter ones. Different sources contradict each other. Wikipedia claims O(1) concatenation. This page says that concatenation is O(1) only when one operand is small and O(log N) otherwise. So, what is the time complexity of concatenation? When exactly rebalancing is performed to ensure this concatenation

Is std::container::size_type guaranteed to be size_t for standard containers with default allocator?

对着背影说爱祢 提交于 2020-01-01 09:35:06
问题 Like: std::string<T>::size_type std::list<T>::size_type std::map<T>::size_type std::vector<T>::size_type etc. Both cplusplus.com and cppreference.com say that they are usually size_t , but are they truly, unambiguously guaranteed by the standard to be size_t unless a custom allocator is used? 回答1: For STL-containers - nope. Table 96 of the standard in [container.requirements.general], which lists container requirements for any container X , explains it pretty clear: However, for basic_string

C++ STL container and in-place construction

耗尽温柔 提交于 2020-01-01 09:27:30
问题 Please consider the following: class CMyClass { public: CMyClass() { printf( "Constructor\n" ); } CMyClass( const CMyClass& ) { printf( "Copy constructor\n" ); } }; int main() { std::list<CMyClass> listMyClass; listMyClass.resize( 1 ); return 0; } It produces the following output: Constructor Copy constructor Now my question is: How do I avoid the copy constructor? Or to put it in another way: How can I create objects inside an STL container without the unnecessary copy operation. Is there

Hashing pointers as Keys for unordered_map in C++ STL

一曲冷凌霜 提交于 2020-01-01 09:23:40
问题 I posted a similar quetion regarding using pointers as Keys on maps in C++ STL. How are pointers hashed in unordered_maps when used as Keys. More specifically if I define: std::unordered_map< CustomClass*, int > foo; Would the default C++ std::hash implementation work to handle these pointers? Is it safe to use? Is this good practice? 回答1: std::hash<T*> is defined but the details of how it operates are implementation dependent. It will certainly be safe to use, and I'd consider it good

why there is no find for vector in C++

空扰寡人 提交于 2020-01-01 09:18:21
问题 what's the alternative? Should I write by myself? 回答1: There is the std::find() algorithm, which performs a linear search over an iterator range, e.g., std::vector<int> v; // Finds the first element in the vector that has the value 42: // If there is no such value, it == v.end() std::vector<int>::const_iterator it = std::find(v.begin(), v.end(), 42); If your vector is sorted, you can use std::binary_search() to test whether a value is present in the vector, and std::equal_range() to get begin