Connecting Tech Pros Worldwide Help | Site Map

Setting first and second of pair<>

  #1  
Old July 23rd, 2005, 06:49 AM
pmatos
Guest
 
Posts: n/a
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, 06:49 AM
John Carson
Guest
 
Posts: n/a

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, 06:49 AM
Rolf Magnus
Guest
 
Posts: n/a

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, 06:49 AM
pmatos
Guest
 
Posts: n/a

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, 06:49 AM
John Carson
Guest
 
Posts: n/a

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, 06:49 AM
benben
Guest
 
Posts: n/a

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, 06:49 AM
pmatos
Guest
 
Posts: n/a

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]

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
learner's question on populating vector< pair<int, string>* > asmember subramanian100in@yahoo.com, India answers 18 June 27th, 2008 05:41 PM
Why are there *three* constructor calls when inserting an item intoa map<>? Szabolcs Horvát answers 10 October 10th, 2006 04:15 AM