Setting first and second of pair<> 
July 23rd, 2005, 05:49 AM
| | | 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 | 
July 23rd, 2005, 05:49 AM
| | | 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 | 
July 23rd, 2005, 05:49 AM
| | | 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? | 
July 23rd, 2005, 05:49 AM
| | | 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] | 
July 23rd, 2005, 05:49 AM
| | | 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 | 
July 23rd, 2005, 05:49 AM
| | | 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. | 
July 23rd, 2005, 05:49 AM
| | | 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] | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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.
|