samedi 29 décembre 2018

how to get a pointer to head of std::set in c++

how can i get a pointer to the first element in a set

   int main()
   {
    std::vector<int> v ;
    v.insert(v.end() , 1);
    v.insert(v.end() , 2);
    v.insert(v.end() , 5);
    v.insert(v.end() , 4);

    std::set<int> s ;

    s.insert(s.end() , 754);
    s.insert(s.end() , 5);
    s.insert(s.end() , 3);
    s.insert(s.end() , 4);

    std::list<int> l ;

    l.insert(l.end() , 45);
    l.insert(l.end() , 5);
    l.insert(l.end() , 3);
    l.insert(l.end() , 4);

    int *p = (int*)(&*l.begin());  <<<<<<-------- ( here )
    cout << *p++ <<endl;
    cout << *p++ <<endl;
    cout << *p++ <<endl;
    cout << *p <<endl;

it work with vectors but it gives me a junk value with set and list any idea ??

Aucun commentaire:

Enregistrer un commentaire