Connecting Tech Pros Worldwide Help | Site Map

Setting first and second of pair<>

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 05:49 AM
pmatos
Guest
 
Posts: n/a
Default Setting first and second of pair<>

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


  #2  
Old July 23rd, 2005, 05:49 AM
John Carson
Guest
 
Posts: n/a
Default Re: Setting first and second of pair<>

"pmatos" <pocm@sat.inesc-id.pt> wrote in message
news:1120392754.156107.307180@f14g2000cwb.googlegr oups.com[color=blue]
> 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)?[/color]

#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

  #3  
Old July 23rd, 2005, 05:49 AM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Setting first and second of pair<>

pmatos wrote:
[color=blue]
> Hi all,
>
> Is there a way of (after creating a pair) set its first and second
> element of not?[/color]

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';
}
[color=blue]
> (pair is definitely a read-only struct)?[/color]

It is not. What makes you think so?
  #4  
Old July 23rd, 2005, 05:49 AM
pmatos
Guest
 
Posts: n/a
Default Re: Setting first and second of pair<>

John Carson wrote:[color=blue]
> "pmatos" <pocm@sat.inesc-id.pt> wrote in message
> news:1120392754.156107.307180@f14g2000cwb.googlegr oups.com[color=green]
> > 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)?[/color]
>
> #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;
> }
>
>[/color]

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?


[color=blue]
> --
> John Carson[/color]

  #5  
Old July 23rd, 2005, 05:49 AM
John Carson
Guest
 
Posts: n/a
Default Re: Setting first and second of pair<>

"pmatos" <pocm@sat.inesc-id.pt> wrote in message
news:1120395324.583394.140060@g49g2000cwa.googlegr oups.com[color=blue]
> John Carson wrote:
>[color=green]
>> #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;
>> }
>>
>>[/color]
>
> 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.[/color]


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

  #6  
Old July 23rd, 2005, 05:49 AM
benben
Guest
 
Posts: n/a
Default Re: Setting first and second of pair<>

[color=blue]
> That's wierd.
> If I have:
> list<pair<int, int> > l;
> l.push_back(pair<int,int>(1,1));[/color]

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

I compiled the code and it runs fine. Is it a compiler-error? "read-only
structure" is not a common term to me.


  #7  
Old July 23rd, 2005, 05:49 AM
pmatos
Guest
 
Posts: n/a
Default Re: Setting first and second of pair<>

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

Cheers,

Paulo Matos

benben wrote:[color=blue][color=green]
> > That's wierd.
> > If I have:
> > list<pair<int, int> > l;
> > l.push_back(pair<int,int>(1,1));[/color]
>
> or l.push_back(make_pair(1, 1));
>[color=green]
> > l.begin()->first = 2;
> >
> > Says the pair is a read-only structure.
> >
> > Any ideas?
> >[/color]
>
> I compiled the code and it runs fine. Is it a compiler-error? "read-only
> structure" is not a common term to me.[/color]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.