473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2744
"pmatos" <po**@sat.ine sc-id.pt> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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.ine sc-id.pt> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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(pai r<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.ine sc-id.pt> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(pai r<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(pai r<int,int>(1,1) );
or l.push_back(mak e_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(pai r<int,int>(1,1) );


or l.push_back(mak e_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
45829
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 on what you are using the pair for. For instance if I am using coordinates in two dimensional space then I like to use x and y. So I might as well define my own struct with x and y members in it and create a constructor so
10
2962
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 constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The output of the program is: constructor 0 constructor 1
6
1512
by: asdf | last post by:
It is best to show me some examples. Thanks a lot.
18
2499
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 container. I have written both these operators as non-friend functions.
9
3714
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 = it->func().second->anObj(); } else { myObj = it->func().first->anObj(); }
10
3771
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
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.