Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert an element to std::set using constructor

Tags:

c++

stl

is it possible to insert a new element to std::set like in case of std::list for example:

//insert one element named "string" to sublist of mylist

std::list< std::list<string> > mylist;
mylist.push_back(std::list<string>(1, "string"));

Now, mylist has one element of type std::string in its sub-list of type std::list.

How can you do the same in if std::set is the sub-set of std::list my list i.e

std::list<std::set <string>> mylist;

if you can't then why not?

like image 632
cpx Avatar asked Feb 28 '23 10:02

cpx


1 Answers

I think this should do the trick:

int main()
{
    string s = "test";
    set<string> mySet(&s, &s+1);

    cout << mySet.size() << " " << *mySet.begin();

    return 0;
}

For clarification on the legality and validity of treating &s as an array, see this discussion: string s; &s+1; Legal? UB?

like image 179
John Dibling Avatar answered Mar 06 '23 19:03

John Dibling