std

How to make a c++11 std::unordered_set of std::weak_ptr

自作多情 提交于 2019-12-30 08:13:17
问题 I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName; It works fine. But I would like to change it to an unordered set. However, I get about six pages of errors when I do that. Any ideas how to do that? After looking through all the pages of error messages I found to lines that might help. /usr/include/c++/4.7/bits/functional_hash.h:60:7: error: static assertion failed: std::hash is not specialized for this type /usr/include/c++/4.7/bits/stl_function.h: In

Does libstdc++ not implement std::stoi?

↘锁芯ラ 提交于 2019-12-30 07:57:52
问题 I want to use std::stoi . Although I could use ::atoi(str.c_str()) it would make the code cleaner if this would work. But Eclipse tells me: Function 'stoi' could not be resolved I checked that the header <string> is included, include paths are set correctly, as I can use std::string , the compiler flag -std=c++0x -std=c++11 is set too. Is stoi() missing in gcc, or is it somehow my fault? I am using gcc (Debian 4.7.2-4) 4.7.2. 回答1: You're using GCC 4.7.2, so std::stoi is supported. You can

C++ vector emplace_back calls copy constructor

二次信任 提交于 2019-12-30 06:14:45
问题 This is a demo class. I do not want my class to be copied, so I delete the copy constructor. I want vector.emplace_back to use this constructor 'MyClass(Type type)'. But these codes won't compile. Why? class MyClass { public: typedef enum { e1, e2 } Type; private: Type _type; MyClass(const MyClass& other) = delete; // no copy public: MyClass(): _type(e1) {}; MyClass(Type type): _type(type) { /* the constructor I wanted. */ }; }; std::vector<MyClass> list; list.emplace_back(MyClass::e1); list

C++ vector emplace_back calls copy constructor

时光怂恿深爱的人放手 提交于 2019-12-30 06:14:26
问题 This is a demo class. I do not want my class to be copied, so I delete the copy constructor. I want vector.emplace_back to use this constructor 'MyClass(Type type)'. But these codes won't compile. Why? class MyClass { public: typedef enum { e1, e2 } Type; private: Type _type; MyClass(const MyClass& other) = delete; // no copy public: MyClass(): _type(e1) {}; MyClass(Type type): _type(type) { /* the constructor I wanted. */ }; }; std::vector<MyClass> list; list.emplace_back(MyClass::e1); list

variable-length std::array like

孤者浪人 提交于 2019-12-30 02:45:08
问题 As my usually used C++ compilers allow variable-length arrays (eg. arrays depending on runtime size), I wonder if there is something like std::array with variable size? Of course std::vector is of variable size, but it allocates on heap, and reallocates on need. I like to have a stack allocated array with size defined at runtime. Is there any std -template that may feature this? Maybe using std::vector with a fixed maximal size? 回答1: There are two proposals currently being worked on to bring

How to read a file into unsigned char array from std::ifstream?

旧巷老猫 提交于 2019-12-30 02:28:12
问题 So normaly I do stuff like: std::ifstream stream; int buff_length = 8192; boost::shared_array<char> buffer( new char[buff_length]); stream.open( path.string().c_str(), std::ios_base::binary); while (stream) { stream.read(buffer.get(), buff_length); //boost::asio::write(*socket, boost::asio::buffer(buffer.get(), stream.gcount())); } stream.close(); I wonder how to read into unsigned char buffer ( boost::shared_array<unsigned char> buffer( new unsigned char[buff_length]); ) 回答1: In a simplest

C++ STL map: is access time O(1)?

﹥>﹥吖頭↗ 提交于 2019-12-30 01:36:35
问题 Is key look up on std::map O(1)? I thought it was until I thought about it more. It is based on a tree implementation so the lookup time should be O(log N), correct? And, is it possible to have O(1) look up on string key, std::unordered_map perhaps? 回答1: The complexity of lookup for std::map is O(log N) (logarithmic in the size of the container). Per Paragraph 23.4.4.3/4 of the C++11 Standard on std::map::operator [] : Complexity : logarithmic. The complexity of lookup for std::unordered_map

std vector C++ — deep or shallow copy

主宰稳场 提交于 2019-12-29 19:07:12
问题 I wonder whether copying a vector I am copying the vector with its values (whereas this is not working with array, and deep copy need a loop or memcpy). Could you hint to an explanation? Regards 回答1: You are making a deep copy any time you copy a vector. But if your vector is a vector of pointers you are getting the copy of pointers, not the values are pointed to For example: std::vector<Foo> f; std::vector<Foo> cp = f; //deep copy. All Foo copied std::vector<Foo*> f; std::vector<Foo*> cp = f

std vector C++ — deep or shallow copy

梦想的初衷 提交于 2019-12-29 19:05:46
问题 I wonder whether copying a vector I am copying the vector with its values (whereas this is not working with array, and deep copy need a loop or memcpy). Could you hint to an explanation? Regards 回答1: You are making a deep copy any time you copy a vector. But if your vector is a vector of pointers you are getting the copy of pointers, not the values are pointed to For example: std::vector<Foo> f; std::vector<Foo> cp = f; //deep copy. All Foo copied std::vector<Foo*> f; std::vector<Foo*> cp = f

Move constructor is required even if it is not used. Why?

本小妞迷上赌 提交于 2019-12-29 08:50:51
问题 Why?! Why C++ requires the class to be movable even if it's not used! For example: #include <iostream> using namespace std; struct A { const int idx; // It could not be compileld if I comment out the next line and uncomment // the line after the next but the moving constructor is NOT called anyway! A(A&& a) : idx(a.idx) { cout<<"Moving constructor with idx="<<idx<<endl; } // A(A&& a) = delete; A(const int i) : idx(i) { cout<<"Constructor with idx="<<i<<endl; } ~A() { cout<<"Destructor with