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 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
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?
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
"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 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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
by: asdf |
last post by:
It is best to show me some examples.
Thanks a lot.
|
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.
|
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();
}
| |
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |