Setting first and second of pair<>

P

pmatos

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
 
J

John Carson

pmatos said:
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;
}
 
R

Rolf Magnus

pmatos said:
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?
 
P

pmatos

John said:
#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?


 
J

John Carson

pmatos said:
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;

?
 
B

benben

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.
 
P

pmatos

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

Cheers,

Paulo Matos
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top