1

I'm looking for a way to insert a vector of elements into an unordered set that I have already declared. Say I have code as follows:

unordered_set<string> us;
vector<string> v{"green", "dog", "keys"};

Here, us has already been declared. How can I populate us with the elements in vector v with one command (i.e. without a for loop and pushing the elements individually) after its declaration?

This similar answer adding elements of a vector to an unordered set is not what I'm looking for as the unordered set is initialized during declaration.

Better yet, is it possible to populate the unordered set with multiple elements in one command without using a vector?

rjc810
  • 425
  • 1
  • 3
  • 17

1 Answers1

4

Use an iterator with insert().

us.insert(v.cbegin(), v.cend());

No need for the vector as long as the sequence conforms to the input iterator concept and returns strings. An array, other set, vector, etc. are all fine.

beta
  • 647
  • 5
  • 19
  • would it work with no declaration at all, like just a list of elements `{"green", "dog", "keys"}`. Similar to how an unordered set can be initialized by `unordered_set us ({"green", "dog", "keys"});` – rjc810 Jul 15 '21 at 04:07
  • Yeah, an initializer list would work as well, both during construction and with insert. – beta Jul 15 '21 at 04:09
  • I'm getting the error `calc.cpp:172:23: error: expected expression constants.insert ({"e", "pi", "i"});`. – rjc810 Jul 15 '21 at 04:12
  • Insert with an initializer list is a C++11 feature. You’ll need to enable C++11 in your compile options. A piece of advice, though: you ideally shouldn’t be populating the set after the unordered set is constructed. That set should be able to be marked const. – beta Jul 15 '21 at 04:15
  • https://pastebin.com/5qUS6Ycc Oh, okay. Is it not set by default? – rjc810 Jul 15 '21 at 04:15
  • It’s usually not. C++ compilers always err on the side of caution. – beta Jul 15 '21 at 04:17
  • Pretty sure insert with initializer list was in C++11. [Quick hack code agrees with me](https://godbolt.org/z/j48YxhhM6). – user4581301 Jul 15 '21 at 04:17
  • And I'm dumb. I could have looked it up instead of hacking out code. – user4581301 Jul 15 '21 at 04:19
  • @user4581301 @beta so should the paste work under default compilation settings? currently im just doing `gcc filename.cpp -o output ./output` – rjc810 Jul 15 '21 at 04:20
  • 1
    @rjc810 you've forgotten to `#include`. [More quick hack code](https://godbolt.org/z/3fK1xKjhe) – user4581301 Jul 15 '21 at 04:20
  • 2
    You need to add -std=c++11 to the arguments. – beta Jul 15 '21 at 04:21
  • 1
    Note on the compiler options: add `-Wall -Wextra` to get more diagnostic messages from the compiler. If it can spot potential screw ups you can fix them without having to stumble over them while debugging. Also consider adding `-O2` or `-O3` to crank up the optimizer. When the compiler optimizes it takes a longer and deeper look at your code in order to optimize it. That longer, deeper look also often spots even more things the compiler can warn you about. – user4581301 Jul 15 '21 at 04:32