inserting elements in a set with type pair

I want to use a dataset of type set which will have the type pair<char,string> or pair<string,string>.
How can i insert values into the set, because i have to initialize the set and will not change the set during the program.
please help quickly.
thanks.
i have to initialize the set and will not change the set during the program.

If you want it to be const, you could initialize it this way:

1
2
3
4
5
6
7
8
9
10
#include <set>
#include <iostream>
#include <string>

const std::set<std::pair<char, std::string>> s = {{'a', "aaa"}, {'b', "bbb"}};
int main()
{
   for(auto& p: s)
       std::cout << p.first << " : " << p.second << '\n';
}

demo: http://liveworkspace.org/code/ezwW3

On older compilers, you might have to write more code:

1
2
3
const std::pair<char, std::string> a[] = { std::make_pair('a', "aaa"),
                                           std::make_pair('b', "bbb") };
const std::set<std::pair<char, std::string>> s(a, a+2);
Last edited on
thanks for the reply.
i have to put the -std=c++0x to compile it.
Asking if gcc has supported the c++11 completely.
if yes which version. mine is 4.6.3
No compilers completely support c++11.
Here is the table of support of c++11 features:
http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport
Topic archived. No new replies allowed.