std

C++ Long switch statement or look up with a map?

半城伤御伤魂 提交于 2019-12-03 22:07:31
In my C++ application, I have some values that act as codes to represent other values. To translate the codes, I've been debating between using a switch statement or an stl map. The switch would look something like this: int code; int value; switch(code) { case 1: value = 10; break; case 2: value = 15; break; } The map would be an stl::map<int, int> and translation would be a simple lookup with the code used as the key value. Which one is better/more efficient/cleaner/accepted? Why? Personally, I would use the map, as its use implies a data lookup - using a switch usually indicates a

What are the default Format Flags (and widths) for double output in a std::stringstream?

浪子不回头ぞ 提交于 2019-12-03 21:35:02
What is the default format, when I'm writting a double to a stringstream ? double v = 3.0; std::stringstream ss; ss << v; Where can I find a list of the default format setup for a stringstream ? Is the default format the same for all derived classes of std::istream (within the stdlib)? The defaults are setup by std::basic_ios::init and are the same for all streams derived from ios_base . The defaults are: rdbuf() sb tie() 0 rdstate() goodbit if sb is not a null pointer, otherwise badbit. exceptions() goodbit flags() skipws | dec width() 0 precision() 6 fill() widen(’ ’) getloc() a copy of the

How to convert QByteArray to std::istream or std::ifstream?

我的梦境 提交于 2019-12-03 18:10:51
问题 I want to create istream from QByteArray at runtime, without saving a physical file in memory of QByteArray . I found that there are many ways to do the opposite conversion, i.e. istream to QByteArray , but not this one. How to accomplish that? 回答1: To read via std::istringstream from QByteArray seems quite easy: testQByteArray-istream.cc : #include <iostream> #include <sstream> #include <QtCore> int main() { qDebug() << "Qt Version:" << QT_VERSION_STR; // make a QByteArray QByteArray data(

std::default_random_engine generate values between 0.0 and 1.0

不想你离开。 提交于 2019-12-03 18:03:04
问题 I want to be able to generate random values between 0.0 and 1.0 I've tried to use std::default_random_engine generator; std::uniform_real_distribution<float> distribution(0.0, 1.0); float myrand = distribution(generator); Generating random value in a loop gives me always these values: 0.000022 0.085032 0.601353 0.891611 0.967956 0.189690 0.514976 0.398008 0.262906 0.743512 0.089548 What can I do to really get random values? Doesn't seem that random if I always get the same ones. 回答1: // 1-st

C++ std::async run on main thread

非 Y 不嫁゛ 提交于 2019-12-03 16:49:05
问题 IS there a way of running a function back on the main thread ? So if I called a function via Async that downloaded a file and then parsed the data. It would then call a callback function which would run on my main UI thread and update the UI ? I know threads are equal in the default C++ implementation so would I have to create a shared pointer to my main thread. How would I do this and pass the Async function not only the shared pointer to the main thread but also a pointer to the function I

Is max(a,b) defined in stdlib.h or not?

99封情书 提交于 2019-12-03 16:17:12
问题 I'm using two computers, each with a different version of visual studio. On the visual studio 2008 computer my code compiles. On the visual 2010 computer my code doesn't compile because I'm using the macro max(a,b) which as far as I know is defined in stdlib.h. I cannot just define max(a,b) because it'll be a redefinition on the visual 2008 computer. But if I don't define max(a,b) my code doesn't compile on the visual 2010 computer. Any solution? 回答1: Any C library which defines a macro named

How can I discover the size/length(in bytes) of a std::vector?

被刻印的时光 ゝ 提交于 2019-12-03 16:11:17
问题 I have a vector and I want to write and read it to a file but it is not possible to determine the logical size of a vector using the sizeof operator. So what shall I do? 回答1: A c++ std::vector has a method size() which returns its size. EDIT: as I get it now you need to compute the memory a given vector uses. You can't use sizeof for that as a vector uses dynamic memory and stores only a pointer of a dynamic array containing its elements. So my best suggestion would be to multiply the memory

Why is std::type_info noncopyable? Am I allowed to store it somewhere?

落爺英雄遲暮 提交于 2019-12-03 15:54:06
问题 The std::type_info class is non-copyable. This makes it hard to store it in an object for later use. What should I do? 回答1: There is a much better solution in C++11. A new copyable wrapper called std::type_index. You need to include header "typeindex" to use it. 回答2: You can store a pointer to a constant std::type_info object. 回答3: From MSDN and IBM online documentation: The type_info class describes type information generated within the program by the compiler. Objects of this class

When to use std::string vs char*? [duplicate]

最后都变了- 提交于 2019-12-03 14:46:53
This question already has answers here : char* vs std::string in c++ [closed] (12 answers) Possible Duplicate: C++ char* vs std::string I'm new to C++ coming from C# but I really do like C++ much better. I have an abstract class that defines two constant strings (not static). And I wondered if a const char* would be a better choice. I'm still getting the hang of the C++ standards, but I just figured that there really isn't any reason why I would need to use std::string in this particular case (no appending or editing the string, just writing to the console via printf ). Should I stick to std:

How to convert std::queue to std::vector

耗尽温柔 提交于 2019-12-03 14:24:11
I need to make use of a queue of doubles because of the good properties it has as an ordered container. I want to pass this queue to a class constructor that accepts vectors. If I do that directly I get the following error: candidate constructor not viable: no known conversion from 'std::queue' to 'std::vector &' for 2nd argument How to cast a queue to a vector? The correct container to model both queue_like behaviour and vector-like behaviour is a std::deque . This has the advantages of: constant-time insertion and deletion at either end of the deque ability to iterate elements without