c++-standard-library

How to initialize std stack with std vector?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 18:19:08
问题 I need to put an std::vector into an std::stack . Here is my method so far(I am building a card game) : void CardStack::initializeCardStack(std::vector<Card> & p_cardVector) { m_cardStack = std::stack<Card>(); //code that should initialize m_cardStack with p_cardVector } Note : I cannot change my method signature because it is a imposed by a teacher... Do I have to iterate over the whole vector ? What is the most efficient way to do this ? The documentation. I have tried Jens answer but it

Why do we have std::string::npos but no std::vector::npos?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 17:53:52
问题 I would like to use -1 to indicate a size that has not yet been computed: std::vector<std::size_t> sizes(nResults, -1); and I was wondering why isn't there a more expressive way: std::vector<std::size_t> sizes(nResults, std::vector<std::size_t>::npos); 回答1: From cppreference: std::size_t is the unsigned integer type of the result of the sizeof operator as well as the sizeof operator and the alignof operator (since C++11).... ...std::size_t can store the maximum size of a theoretically

How is this shared_ptr automatically converted to a raw pointer?

女生的网名这么多〃 提交于 2019-12-10 17:37:37
问题 I'm studying enable_shared_from_this of C++11 now; one example made me confused: how the shared_ptr type returned by shared_from_this() can convert to this raw pointer? #include <iostream> #include <memory> #include <functional> struct Bar { Bar(int a) : a(a) {} int a; }; struct Foo : public std::enable_shared_from_this<Foo> { Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } std::shared_ptr<Bar> getBar(int a) { std::shared_ptr<Bar> pb( new Bar{a}, std::bind(&Foo:

Can std::forward_list members be implemented as static?

人走茶凉 提交于 2019-12-10 12:43:03
问题 std::forward_list provides insert_after and erase_after members which may not need to actually access the std::forward_list object. Therefore they can be implemented as static member functions and be called without a list object — useful for an object that wants to delete itself from a list, which is a very common use. EDIT : This optimization only applies to forward_list specializations on std::allocator or user-defined stateless allocators. Can a standard-conforming implementation do this?

Why is there no language support in C++ for all C++ standard library type traits?

核能气质少年 提交于 2019-12-10 10:24:41
问题 In C++ it is impossible to implement certain C++ standard library type traits without compiler intrinsics, using the C++ language only. Traits deal directly with C++ types. According to §17.6.1.3.2 freestanding implementations of the C++ standard library must implement <type_traits> . Doesn't this effectively mean that the C++ standard requires non-standard language extensions/compiler intrinsics from all compilers which support want to support freestanding C++ standard library

Is there a clean separating definition between “STL” and “C++ Standard Library”? [duplicate]

喜欢而已 提交于 2019-12-10 07:01:44
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What's this STL vs. “C++ Standard Library” fight all about? I am very much used to the term STL ("Standard Template Library") and I catch myself often using it when I really mean the C++ Standard Library . So, since almost everything in the C++(-11) Standard Library is a template nowadays, I wonder: Is there a definition what is STL and what is not, in the C++Standard-Lib? Maybe containers, streams, algorithms,

How to Fix Visual Studio 2012 error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string?

爷,独闯天下 提交于 2019-12-10 01:50:26
问题 How to fix a Visual Studio 2012 error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string? I've been compiling a solution containing one exe and several static lib projects on which the exe depends fine with Visual Studio 2008. The static libs include: TCL: tcl84tsx.lib wxWidgets: wxbase.lib zlib.lib ws2_32.lib xerces-c_2.lib SNMP Research EMANATE: subagent.lib,agent.lib,emanate.lib,pack.lib,mibtable.lib,devkit.lib etc. I loaded the solution into Visual

Can reserved function names be overloaded?

ぃ、小莉子 提交于 2019-12-09 16:32:55
问题 This question is a follow-up question of this one. Consider the following program: #include <cmath> // meaningless, only for language-lawyer purpose void abs(void*) {} int main(){ abs(nullptr); } Does this program result in undefined behavior? The related part in the standard is [extern.names]/4: Each function signature from the C standard library declared with external linkage is reserved to the implementation for use as a function signature with both extern "C" and extern "C++" linkage, or

are there any plans in C++ standard to address inconsistency of initializer list constructors?

北城余情 提交于 2019-12-09 14:52:35
问题 initializer list constructors in C++ often cause trouble; for example using std::vector; using std::string; vector<string> v{3}; // vector of three empty strings vector<int> u{3}; // vector of one element with value 3 (Just to clarify, I mean <int> constructor is an initializer list constructor, while the <string> one is not .) The int case matches the initializer list constructor, while the string case doesn't. This is somewhat ugly, and often causes trouble. It was also noted in an early

Gnu C++ macro __cplusplus standard conform?

依然范特西╮ 提交于 2019-12-09 07:34:51
问题 The Gnu C++ compiler seems to define __cplusplus to be 1 #include <iostream> int main() { std::cout << __cplusplus << std::endl; } This prints 1 with gcc in standard c++ mode, as well as in C++0x mode, with gcc 4.3.4, and gcc 4.7.0. The C++11 FDIS says in "16.8 Predefined macro names [cpp.predefined]" that The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit. (Footnote: It is intended that future versions of this standard will replace the value of this