473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

Jul 23 '05 #1
6 2729
"pmatos" <po**@sat.inesc-id.pt> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
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;
}
--
John Carson

Jul 23 '05 #2
pmatos wrote:
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?
Jul 23 '05 #3
John Carson wrote:
"pmatos" <po**@sat.inesc-id.pt> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
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;
}


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?
--
John Carson


Jul 23 '05 #4
"pmatos" <po**@sat.inesc-id.pt> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com
John Carson wrote:
#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.

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

Jul 23 '05 #5
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.
Jul 23 '05 #6
Thanks all, the problems was that I was accessing it through
const_iterator to list. :)

Cheers,

Paulo Matos

benben wrote:
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.


Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Neil Zanella | last post by:
Hello, I would like to ask how come the design of C++ includes std::pair. First of all I don't think many programmers would use it. For starters, what the first and second members are depends...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
6
by: asdf | last post by:
It is best to show me some examples. Thanks a lot.
18
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the...
9
by: shaun roe | last post by:
Question about pointer-to-data members I have a deeply nested loop, the innermost bit looks a little like this: for(it(vec.begin(), end(vec.end()), it!=end;++it){ if (global == 0){ myObj =...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.