Connecting Tech Pros Worldwide Forums | Help | Site Map

Setting first and second of pair<>

pmatos
Guest
 
Posts: n/a
#1: Jul 23 '05
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


John Carson
Guest
 
Posts: n/a
#2: Jul 23 '05

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

Rolf Magnus
Guest
 
Posts: n/a
#3: Jul 23 '05

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?
pmatos
Guest
 
Posts: n/a
#4: Jul 23 '05

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]

John Carson
Guest
 
Posts: n/a
#5: Jul 23 '05

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

benben
Guest
 
Posts: n/a
#6: Jul 23 '05

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.


pmatos
Guest
 
Posts: n/a
#7: Jul 23 '05

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