stl

How to cout the std::basic_string<TCHAR>

孤者浪人 提交于 2019-12-29 05:55:22
问题 I am trying to cout a basic_string<TCHAR> . But cout is throwing error. Can I know how to do that 回答1: As dauphic said, std::wcout is for wide strings and std::cout for narrow ones. If you want to be able to compile for either type of string ( TCHAR is meant to make this sort of thing easier) something like this sometimes makes life easier: #if defined(UNICODE) || defined(_UNICODE) #define tcout std::wcout #else #define tcout std::cout #endif With this in place use tcout instead. 回答2: TCHAR

How to cout the std::basic_string<TCHAR>

做~自己de王妃 提交于 2019-12-29 05:55:05
问题 I am trying to cout a basic_string<TCHAR> . But cout is throwing error. Can I know how to do that 回答1: As dauphic said, std::wcout is for wide strings and std::cout for narrow ones. If you want to be able to compile for either type of string ( TCHAR is meant to make this sort of thing easier) something like this sometimes makes life easier: #if defined(UNICODE) || defined(_UNICODE) #define tcout std::wcout #else #define tcout std::cout #endif With this in place use tcout instead. 回答2: TCHAR

simple C++ templates suited for STL Containers

你。 提交于 2019-12-29 05:37:10
问题 I need a template like this, which work perfectly template <typename container> void mySuperTempalte (const container myCont) { //do something here } then i want to specialize the above template for std::string so i came up with template <typename container> void mySuperTempalte (const container<std::string> myCont) { //check type of container //do something here } which doesnt't work, and throws an error. I would like to make the second example work and then IF possible i would like to add

Initialization of a vector of vectors?

三世轮回 提交于 2019-12-29 04:36:07
问题 Is there a way to initialize a vector of vectors in the same ,quick, manner as you initialize a matrix? typedef int type; type matrix[2][2]= { {1,0},{0,1} }; vector<vector<type> > vectorMatrix; //??? 回答1: For the single vector you can use following: typedef int type; type elements[] = {0,1,2,3,4,5,6,7,8,9}; vector<int> vec(elements, elements + sizeof(elements) / sizeof(type) ); Based on that you could use following: type matrix[2][2]= { {1,0},{0,1} }; vector<int> row_0_vec(matrix[0], matrix[0

Convert “this” pointer to string

泪湿孤枕 提交于 2019-12-29 04:17:45
问题 In a system where registered objects must have unique names, I want to use/include the object's this pointer in the name. I want the simplest way to create ??? where: std::string name = ???(this); 回答1: You could use string representation of the address: #include <sstream> //for std::stringstream #include <string> //for std::string const void * address = static_cast<const void*>(this); std::stringstream ss; ss << address; std::string name = ss.str(); 回答2: You mean format the pointer itself as

Amortized analysis of std::vector insertion

余生长醉 提交于 2019-12-29 03:57:05
问题 How do we do the analysis of insertion at the back (push_back) in a std::vector? It's amortized time is O(1) per insertion. In particular in a video in channel9 by Stephan T Lavavej and in this ( 17:42 onwards ) he says that for optimal performance Microsoft's implementation of this method increases capacity of the vector by around 1.5. How is this constant determined? 回答1: Assuming you mean push_back and not insertion, I believe that the important part is the multiply by some constant (as

Copying std::vector: prefer assignment or std::copy?

为君一笑 提交于 2019-12-29 02:45:07
问题 I have two vectors: std::vector<int> v1, v2; // Filling v1 ... And now I need to copy v1 to v2 . Is there any reason to prefer v2 = v1; to std::copy (v1.begin(), v1.end(), v2.begin()); (or vice versa)? 回答1: Generally I would strongly prefer v2 = v1 : It is shorter and makes the intend more clear std::copy won't work if v2 doesn't have the same length as v1 (it won't resize it, so it will retain some of the old elements best case ( v2.size() > v1.size() and overwrite some random data used

Missing const_iterator overload of std::vector::erase() with g++ 4.8

北战南征 提交于 2019-12-28 20:38:33
问题 The following example will not compile using g++ 4.8.2: #include <iostream> #include <vector> using namespace std; int main() { vector<int> v {1, 2, 3}; v.erase(v.cbegin()); // Compiler complains return 0; } The compiler says the following. (It isn't very readable, but it's complaining that there's not a known conversion between vector<int>::const_iterator and vector<int>::iterator .) prog.cpp: In function ‘int main()’: prog.cpp:8:20: error: no matching function for call to ‘std::vector<int>:

Custom Memory Allocator for STL map

倾然丶 夕夏残阳落幕 提交于 2019-12-28 20:34:53
问题 This question is about construction of instances of custom allocator during insertion into a std::map. Here is a custom allocator for std::map<int,int> along with a small program that uses it: #include <stddef.h> #include <stdio.h> #include <map> #include <typeinfo> class MyPool { public: void * GetNext() { return malloc(24); } void Free(void *ptr) { free(ptr); } }; template<typename T> class MyPoolAlloc { public: static MyPool *pMyPool; typedef size_t size_type; typedef ptrdiff_t difference

C++ stl unordered_map implementation, reference validity

南笙酒味 提交于 2019-12-28 20:33:30
问题 For both std::map and std::tr1::unordered_map , I see from the standard that: References to elements in the unordered_map container remain valid in all cases, even after a rehash. How are they doing that ( implementation-wise )? Are they maintaining all the entries as a kind of linked list and then the hash-table just stores pointers to the elements? 回答1: Yes, linked lists are involved, although not quite in the way you suggest. The 2011 standard says (23.2.5 para 8), "The elements of an