std

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

梦想与她 提交于 2019-12-04 22:51:10
问题 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? 回答1: The correct container to model both queue_like behaviour and vector-like behaviour is a std::deque . This has the advantages of

Inspecting contents of std::vector in Eclipse CDT debugger

爱⌒轻易说出口 提交于 2019-12-04 22:42:10
I'm using Eclipse with the CDT plugin to develop in C++. I'm also using std library to create vectors and I am having an issue while debugging: Eclipse does not allow me to view the content of the vectors. Is there any way to be able to debug it properly? pederpansen Debugging STL containers in Eclipse is not quite straightforward. Please have a look at this question and the answers explaining the reasons for this. The simplest way without fiddling with the GDB for me is this answer , which can be summarized as follows: Expand your vector in the variable view, you should find a nested variable

Why is it better to use std::make_* instead of the constructor?

你。 提交于 2019-12-04 22:34:20
There are some functions in the STL which start with the make_ prefix like std::make_pair , std::make_shared , std::make_unique etc. Why is it a better practice to use them instead of simply using the constructor ? auto pair2 = std::pair< int, double >( 1, 2.0 ); auto pair3 = std::make_pair( 1, 2.0 ); std::shared_ptr< int > pointer1 = std::shared_ptr< int >( new int( 10 ) ); std::shared_ptr< int > pointer2 = std::make_shared< int >( 10 ); I just see that these functions make the code a little shorter, but is that all ? Are there any other advantages ? Are these functions safer to use ? Aside

Why does std::visit take a variable number of variants?

泄露秘密 提交于 2019-12-04 22:30:37
Trying to get more familiar with C++17, I've just noticed std::visit : template <class Visitor, class... Variants> constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars); Why does std::visit not take a single variant, but rather any number of variants? I mean, you can always take some standard library function and have it take multiple parameters with the same role, working on all of them (e.g. std::find() for multiple elements in a container); or you could be taking multiple visitors and using them on the same variant. So, why this specific 'variadification'? To make multiple

Is there anything like “std::and” or “std::or”?

自作多情 提交于 2019-12-04 22:26:19
Given a container of boolean values (An example is std::vector<bool> ), is there a standard function that returns true if all the values are true ("and") or true if at least one value is true ("or"), with short circuit evalutation ? I digged trough www.cplusplus.com this morning but couldn't find anything close. You can implement by: AND: std::find(vector.begin(), vector.end(), false) == vector.end() // all the values are true OR: std::find(vector.begin(), vector.end(), true) != vector.end() //at least one value is true is there a standard function that returns true if all the values are true

overload vs default parameters in c++ standard

拜拜、爱过 提交于 2019-12-04 18:52:56
问题 I was reading another question, and it got me thinking. Often the standard specifies functions which have default parameters in their descriptions. Does the standard allow writing these as overloads instead? For example, the standard says that std::basic_string::copy has the following declaration: size_type copy(Ch* p, size_type n, size_type pos = 0) const; Could a conforming implementation of the standard library implement this instead as two functions like this? size_type copy(Ch* p, size

What exactly is a namespace and why is it necessary

岁酱吖の 提交于 2019-12-04 18:34:29
问题 I am learning C++ right now, and at the beginning of every project my instructor puts a line that says: using namespace std; I understand that it keeps you from having to call functions in headers you include with their header name like iostream::stdout and instead just call stdout. But what exactly does the line tell C++ to do. What is a namespace and what is std? I am also new to programming besides python so switching to a new paradigm is very confusing for me. 回答1: From cppreference.com:

num_get facet and stringstream conversion to boolean - fails with initialised boolean?

风流意气都作罢 提交于 2019-12-04 17:20:41
I have inherited a template to convert a string to a numerical value, and want to apply it to convert to boolean . I am not very experienced with the stringstream and locale classes. I do seem to be getting some odd behaviour, and I am wondering if someone could please explain it to me? template<typename T> T convertFromString( const string& str ) const { std::stringstream SStream( str ); T num = 0; SStream >> num; return num; } This works fine until I try the boolean conversion string str1("1"); int val1 = convertFromString<int>(str1); // ok string str2("true"); bool val2 = convertFromString

error: cannot convert 'std::basic_string<char>::iterator …' to 'const char* for argument '1' …'

被刻印的时光 ゝ 提交于 2019-12-04 16:42:09
问题 I'm getting the following error: error: cannot convert 'std::basic_string<char>::iterator {aka __gnu_cxx::__normal _iterator<char*, std::basic_string<char> >}' to 'const char*' for argument '1' to 'int remove(const char*)' For some reason, my program compiles perfectly when I'm working on a Mac... but once I use a Linux machine, this error pops up in more than one place. (If anyone could explain why this occurs, that would be great!) Here's one of the instances where the error pops up:

std::vector differences

◇◆丶佛笑我妖孽 提交于 2019-12-04 16:18:52
问题 How does one determine what the differences of 2 vectors are? I have vector<int> v1 and vector<int> v2 ; What I am looking for is a vector<int> vDifferences that contains only elements that are only in v1 or v2 . Is there a standard way to do this? 回答1: Here is the complete and correct answer. Before the set_symmetric_difference algorithm can be used, the source ranges must be ordered: using namespace std; // For brevity, don't do this in your own code... vector<int> v1; vector<int> v2; // ..