stl

Why isn't vector<bool> a STL container?

烈酒焚心 提交于 2020-06-29 04:22:26
问题 Item 18 of Scott Meyers's book Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library says to avoid vector <bool> as it's not an STL container and it doesn't really hold bool s. The following code: vector <bool> v; bool *pb =&v[0]; will not compile, violating a requirement of STL containers. Error: cannot convert 'std::vector<bool>::reference* {aka std::_Bit_reference*}' to 'bool*' in initialization vector<T>::operator [] return type is supposed to be T& , but

C++ What is the role of std::ctype<char>::widen()?

蓝咒 提交于 2020-06-27 06:57:35
问题 According to the C++ standard (§30.7.5.2.4 of C++17 draft (N4659)), out << ch will not perform a widening operation on ch , if ch is a char and out is a std::ostream . Does this imply that std::ctype<char>::widen() (i.e., char -> char ) is guaranteed by the standard to be an identity function ( widen(ch) == ch ) for all characters in the basic source character set? If so, does this, in turn, imply that all locales are required by the standard to use the same non-wide (or multi-byte) encoding

Requirement of operator< constness in std::stable_sort

孤街浪徒 提交于 2020-06-27 04:49:12
问题 I'm a bit confused about the difference in requirements of operator< const qualifier for std::sort and std::stable_sort. Suppose a simple structure: #include <vector> #include <algorithm> struct Custom { bool operator<(const Custom& custom) /* const */{ return true; }; }; Everything is ok if we try to compile and run this code: int main() { std::vector<Custom> values(3); std::sort(values.begin(), values.end()); return 0; } But this code with std::stable_sort failed to compile: int main() {

Requirement of operator< constness in std::stable_sort

我的未来我决定 提交于 2020-06-27 04:48:45
问题 I'm a bit confused about the difference in requirements of operator< const qualifier for std::sort and std::stable_sort. Suppose a simple structure: #include <vector> #include <algorithm> struct Custom { bool operator<(const Custom& custom) /* const */{ return true; }; }; Everything is ok if we try to compile and run this code: int main() { std::vector<Custom> values(3); std::sort(values.begin(), values.end()); return 0; } But this code with std::stable_sort failed to compile: int main() {

Compile errors with #include <string> in Cocoa App

為{幸葍}努か 提交于 2020-06-10 13:46:44
问题 I am trying to compile a Cocoa app in xcode 4.0 and I'm getting this error... fatal error: 'string' file not found ...when trying to compile to .pch file on this line: #include <string> I have another xcode project that does the same thing, but does not get the error. I have scoured the build settings for some different, but I can't find one. The only difference is that the project that compiles OK was started as a command line project, not a Cocoa project, but the build setting are the same.

std::vector vs std::stack

家住魔仙堡 提交于 2020-06-10 07:26:49
问题 What is the difference between std::vector and std::stack ? Obviously vectors can delete items within the collection (albeit much slower than list) whereas the stack is built to be a LIFO-only collection. However, are stacks faster for end-item manipulation? Is it a linked list or dynamically re-allocated array? I can't find much information about stacks, but if I'm picturing them correctly (they are similar to an actual thread stack; push, pop, etc. - along with that top() method) then they

Binary search with returned index in STL?

谁说我不能喝 提交于 2020-06-08 18:51:00
问题 I need a binary search function. I couldn't find any function in the standard library that will return the index of the found item, and if it wasn't found, will return the bitwise complement of the index of the next element that is larger than the item I looked for. What is the function I am looking for? Edit: I need to insert an item to a sorted vector and to keep it sorted. That's why I need to bitwise complement index. 回答1: I'm quite certain the standard library doesn't include anything to

Binary search with returned index in STL?

生来就可爱ヽ(ⅴ<●) 提交于 2020-06-08 18:49:09
问题 I need a binary search function. I couldn't find any function in the standard library that will return the index of the found item, and if it wasn't found, will return the bitwise complement of the index of the next element that is larger than the item I looked for. What is the function I am looking for? Edit: I need to insert an item to a sorted vector and to keep it sorted. That's why I need to bitwise complement index. 回答1: I'm quite certain the standard library doesn't include anything to

std::map - erase last element

≡放荡痞女 提交于 2020-06-08 05:34:29
问题 My map is defined as such: map<string, LocationStruct> myLocations; where the key is a time string I am only keeping 40 items in this map, and would like to drop off the last item in the map when i reach 40 items. I know that i can't do myLocations.erase(myLocations.end()) , so how do i go about this? I do intend for the last item in the map to be the oldest, and therefore FIFO. The data will be coming in rather quick (about 20Hz), so i'm hoping that the map can keep up with it. I do need to

Why can I not pass this comparison function as a template argument?

只愿长相守 提交于 2020-06-02 10:57:22
问题 I am trying to create an std::set with a function I defined for sorting, but I get the error: "Error: function "GFX::MeshCompare" is not a type name" Mesh.h namespace GFX { struct Mesh { [...] }; inline bool MeshCompare(const Mesh& a, const Mesh& b) { return ( (a.pTech < b.pTech) || ( (b.pTech == a.pTech) && (a.pMaterial < b.pMaterial) ) || ( (b.pTech == a.pTech) && (a.pMaterial == b.pMaterial) && (a.topology < b.topology) ) ); } }; Renderer.h namespace GFX { class Renderer { private: [...]