Setting first and second of pair<>

Discussion in 'C++' started by pmatos, Jul 3, 2005.

  1. pmatos

    pmatos Guest

    Hi all,

    Is there a way of (after creating a pair) set its first and second
    element of not? (pair is definitely a read-only struct)?

    Cheers,

    Paulo Matos
     
    pmatos, Jul 3, 2005
    #1
    1. Advertising

  2. pmatos

    John Carson Guest

    "pmatos" <-id.pt> wrote in message
    news:
    > Hi all,
    >
    > Is there a way of (after creating a pair) set its first and second
    > element of not? (pair is definitely a read-only struct)?


    #include <iostream>
    #include <utility>
    using namespace std;

    int main()
    {
    std::pair<int, const char *> p;
    p.first = 5;
    p.second = "C-style string";
    cout << p.first << endl;
    cout << p.second << endl;
    }


    --
    John Carson
     
    John Carson, Jul 3, 2005
    #2
    1. Advertising

  3. pmatos

    Rolf Magnus Guest

    pmatos wrote:

    > Hi all,
    >
    > Is there a way of (after creating a pair) set its first and second
    > element of not?


    Yes.

    #include <utility>
    #include <iostream>

    int main()
    {
    std::pair<int, int> p(1,2);
    std::cout << p.first << '/' << p.second << '\n';
    p.first = 3;
    p.second = 5;
    std::cout << p.first << '/' << p.second << '\n';
    }

    > (pair is definitely a read-only struct)?


    It is not. What makes you think so?
     
    Rolf Magnus, Jul 3, 2005
    #3
  4. pmatos

    pmatos Guest

    John Carson wrote:
    > "pmatos" <-id.pt> wrote in message
    > news:
    > > Hi all,
    > >
    > > Is there a way of (after creating a pair) set its first and second
    > > element of not? (pair is definitely a read-only struct)?

    >
    > #include <iostream>
    > #include <utility>
    > using namespace std;
    >
    > int main()
    > {
    > std::pair<int, const char *> p;
    > p.first = 5;
    > p.second = "C-style string";
    > cout << p.first << endl;
    > cout << p.second << endl;
    > }
    >
    >


    That's wierd.
    If I have:
    list<pair<int, int> > l;
    l.push_back(pair<int,int>(1,1));
    l.begin()->first = 2;

    Says the pair is a read-only structure.

    Any ideas?



    > --
    > John Carson
     
    pmatos, Jul 3, 2005
    #4
  5. pmatos

    John Carson Guest

    "pmatos" <-id.pt> wrote in message
    news:
    > John Carson wrote:
    >
    >> #include <iostream>
    >> #include <utility>
    >> using namespace std;
    >>
    >> int main()
    >> {
    >> std::pair<int, const char *> p;
    >> p.first = 5;
    >> p.second = "C-style string";
    >> cout << p.first << endl;
    >> cout << p.second << endl;
    >> }
    >>
    >>

    >
    > That's wierd.
    > If I have:
    > list<pair<int, int> > l;
    > l.push_back(pair<int,int>(1,1));
    > l.begin()->first = 2;
    >
    > Says the pair is a read-only structure.



    If that is your exact code, then it should work. So either your compiler /
    library version is broken or this is not your exact code.

    What happens with:

    list<int > l;
    l.push_back(1);
    *l.begin() = 2;

    ?

    --
    John Carson
     
    John Carson, Jul 3, 2005
    #5
  6. pmatos

    benben Guest


    > That's wierd.
    > If I have:
    > list<pair<int, int> > l;
    > l.push_back(pair<int,int>(1,1));


    or l.push_back(make_pair(1, 1));

    > l.begin()->first = 2;
    >
    > Says the pair is a read-only structure.
    >
    > Any ideas?
    >


    I compiled the code and it runs fine. Is it a compiler-error? "read-only
    structure" is not a common term to me.
     
    benben, Jul 3, 2005
    #6
  7. pmatos

    pmatos Guest

    Thanks all, the problems was that I was accessing it through
    const_iterator to list. :)

    Cheers,

    Paulo Matos

    benben wrote:
    > > That's wierd.
    > > If I have:
    > > list<pair<int, int> > l;
    > > l.push_back(pair<int,int>(1,1));

    >
    > or l.push_back(make_pair(1, 1));
    >
    > > l.begin()->first = 2;
    > >
    > > Says the pair is a read-only structure.
    > >
    > > Any ideas?
    > >

    >
    > I compiled the code and it runs fine. Is it a compiler-error? "read-only
    > structure" is not a common term to me.
     
    pmatos, Jul 3, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    0
    Views:
    344
    darrel
    Jun 21, 2004
  2. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: need second pair of eyes: databinder.eval problem

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 21, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    432
    darrel
    Jun 21, 2004
  3. Michael
    Replies:
    5
    Views:
    410
    Michael
    Aug 3, 2007
  4. MW
    Replies:
    14
    Views:
    216
    Lori Fleetwood
    Aug 29, 2003
  5. yelipolok
    Replies:
    4
    Views:
    265
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page