Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can std:set (with a single colon) compile?

I accidentally wrote

std::set<string> keys;

as:

std:set<string> keys;

but weirdly enough, Visual Studio 2013 still compiles.

Why does this happen?

Actually keys is not only defined, but later used as a set of strings, such as

if(keys.find(keystr)==keys.end()){
    keys.insert(keystr);
    toret.push_back(tempv);
}
like image 380
athos Avatar asked Aug 28 '18 03:08

athos


People also ask

How is std::set sorted?

std::set is an associative container that contains a sorted set of unique objects of type Key . Sorting is done using the key comparison function Compare. Search, removal, and insertion operations have logarithmic complexity.

Is std::set ordered?

Per the C++ standard, iteration over the elements in an std::set proceeds in sorted order as determined by std::less or by the optional comparison predicate template argument.

Is set associative C++?

Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending.


2 Answers

At block scope, an identifier followed by a single colon introduces a label. Thus, your statement is equivalent to:

set<string> keys;

except that it bears the label std and can be jumped to by the statement goto std;.

For some reason, the name set is known to the compiler---perhaps you did using namespace std;, or using std::set;, or something like that, or perhaps you defined your own set type somewhere.

like image 146
Brian Bi Avatar answered Oct 23 '22 11:10

Brian Bi


In the second case, std is a label. It is the same as spelling default incorrectly in a case statement.

like image 44
cup Avatar answered Oct 23 '22 12:10

cup