std

Iterator for multi-dimensional vector that is used as unidimensional?

≯℡__Kan透↙ 提交于 2020-01-05 06:35:51
问题 I have a vector that looks like this: std::vector<std::vector<MyClass>> myVector; And I would like to access its elements through iterators as if it was an unidimensional vector: for (auto& x : myVector) { foo(x); // x is an object of type MyClass } (i.e. the fact that there are multiple dimensions is transparent to whoever loops through myVector) I have an idea of how this should be done, have a custom iterator implementation that saves current indexes so that when one of the vectors has no

std::string substr method problems

为君一笑 提交于 2020-01-04 14:12:32
问题 Hello I'm writing this method. I want it to extract from a given buffer a portion that is in a given place. I have a string like this something=one;something=two and I want to get "one" This is my idea : static std::string Utils::getHeader( unsigned char * buffer) { std::string *str = new std::string(buffer); std::size_t b_pos = str->find("="); std::size_t a_pos = str->find(";"); return str->substr((a_pos + 1) ,(b_pos + 1)); } but on eclipse I get this error in reference to the std::string

platform-specific std::chrono::high_resolution_clock::period::num

删除回忆录丶 提交于 2020-01-04 05:53:51
问题 I've noticed that std::chrono::high_resolution_clock::period::num = 1 for every system I've tested. Does there exist any system (embedded, desktop, mobile, or otherwise) where it happens to be some other number? (On such a system, 1 second would not be representable in ticks.) 回答1: There are three implementations of std::chrono::high_resolution_clock that I am aware of: Visual Studio, gcc and clang (when used with libc++). All three of these have nanosecond-precision ( std::chrono::high

Why does the upcoming Ranges library not support container initialization from a range?

女生的网名这么多〃 提交于 2020-01-04 04:56:43
问题 Introduction With the upcoming Ranges library, the need to denote a range with two iterators is pretty much gone. For example, instead of if (std::equal(begin(foo), end(foo), begin(bar), end(bar))) we have if (std::ranges::equal(foo, bar)) The latter is arguably superior not only because of its conciseness, but also because it prevents the common pitfall of omitting end(bar) and welcoming bound errors. Problem How about the following code? std::vector<int> vec{begin(foo), end(foo)}; where foo

GCC 4.7.2: std::thread with pointer to member function

和自甴很熟 提交于 2020-01-03 18:32:14
问题 In writing test code for this question I found that the commented line below does not compile on GCC 4.7.2: #include <thread> #include <iostream> struct S { void f() { std::cout << "Calling f()" << std::endl; } }; int main() { S s; // std::thread t(&S::f, s); // does not compile? std::thread t(&S::f, &s); t.join(); } But cppreference seems to claim that the "this" argument can be passed equivalently as an object, reference to object, or pointer to object: If f is pointer to a member function

Is there a standard way to replace a C-style bool array?

喜欢而已 提交于 2020-01-03 17:26:31
问题 In this piece of code void legacyFunction(int length, bool *bitset) { // stuff, lots of stuff } int main() { int somenumber = 6; // somenumber is set to some value here bool *isBitXSet = new bool[somenumber]; // initialisation of isBitXSet. legacyFunction(somenumber, isBitXSet); delete[] isBitXSet; return 0; } I'd like to replace bool *isBitXSet = new bool[somenumber]; by something like std::vector<bool> isBitXset(somenumber, false); But I cannot do legacyFunction(somenumber, isBitXSet.data()

Is there a standard way to replace a C-style bool array?

白昼怎懂夜的黑 提交于 2020-01-03 17:26:06
问题 In this piece of code void legacyFunction(int length, bool *bitset) { // stuff, lots of stuff } int main() { int somenumber = 6; // somenumber is set to some value here bool *isBitXSet = new bool[somenumber]; // initialisation of isBitXSet. legacyFunction(somenumber, isBitXSet); delete[] isBitXSet; return 0; } I'd like to replace bool *isBitXSet = new bool[somenumber]; by something like std::vector<bool> isBitXset(somenumber, false); But I cannot do legacyFunction(somenumber, isBitXSet.data()

Pass member function to C interface requiring callback

烈酒焚心 提交于 2020-01-03 17:22:31
问题 I have an old .dll with plain C interface which takes callbacks to invoke when some work is done. The callback it takes is of type void (f*)(char* arg) . I'm looking for a trick to pass a C++ function object there so that the callback is invoked with "this" pointer stored somewhere, something like bind, but simple bind doesn't work To illustrate this: C interface: typedef void (f*)(char* param) Callback; void registerCallback(Callback c); Usage in C++: class A { void func1() {

Passing std::string in a library API

女生的网名这么多〃 提交于 2020-01-03 15:56:15
问题 We are currently building an API for a certain library. Part of the interface requires the library to get and return to the user classes such as vector and string. When trying to simulate use of the library in a simple scenario, in debug mode the system crush when delivering a string as an input. I believe there is a different representation of the string class in debug or release mode. Then our library assumes to receive a certain representation, read a data member incorrectly and crush

Support of std::cbegin() in C++14

雨燕双飞 提交于 2020-01-03 13:35:45
问题 Item 13 from Scott Mayers' "Effective Modern C++" states to prefer const_iterators over iterators. I agree but I also want to use non-member functions rather than member functions. According to the book there should be a non-member function std::cbegin() and std::cend() in C++14. To make use of this functions I just installed gcc version 4.9.2 and compiled with the flag -std=c++14 . It seems to compile until I try to use std::cbegin() . I start searching for the support for this function but