473,386 Members | 1,815 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,386 software developers and data experts.

getting reference to a STL map element

Jan
Hi there,
yes im a STL newbie, but thanks for replying.
I want to get a reference to an item included in a STL map. e.g.:

map<type1, type2> myMap;
.....
type2& aName = myMap[type1];
.....

what's wrong with this ;-) ? I don't want to have a copy of type2. only
a
reference to it (and if type1 is not in the map, it should be
automatically be
created)

thanks,
Jan

Dec 12 '05 #1
2 1359
Jan wrote:
yes im a STL newbie, but thanks for replying.
I want to get a reference to an item included in a STL map. e.g.:

map<type1, type2> myMap;
....
type2& aName = myMap[type1];
....

what's wrong with this ;-) ?
You cannot use 'type1' in the expression, that's what's wrong. Read
the FAQ, especially 5.8.
I don't want to have a copy of type2. only
a
reference to it (and if type1 is not in the map, it should be
automatically be
created)


Try your best to write your program, then if you get errors, post back
with them (read the suggested FAQ).

V
Dec 12 '05 #2
Jan <Ja*********@t-online.de> wrote:
Hi there,
yes im a STL newbie, but thanks for replying.
I want to get a reference to an item included in a STL map. e.g.:

map<type1, type2> myMap;
....
type2& aName = myMap[type1];
....

what's wrong with this ;-) ?
You cannot index into a map with just a type. For example, the
following works for me:

#include <iostream>
#include <map>

typedef int type1;
typedef char type2;

int main()
{
std::map<type1, type2> myMap;
type1 key = 5;

// use explicit key
type2& aName = myMap[key];
aName = 'c';
std::cout << "myMap[key] = " << myMap[key] << '\n';

// use default value of type1 as the key (in this case, 0)
type2& anotherName = myMap[type1()];
anotherName = 'b';
std::cout << "myMap[type1()] = " << myMap[type1()] << '\n';

return 0;
}
I don't want to have a copy of type2. only
a
reference to it (and if type1 is not in the map, it should be
automatically be
created)


--
Marcus Kwok
Dec 12 '05 #3

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

Similar topics

2
by: Ricardo Garcia | last post by:
hi, i was wondering how to get the size of a text element in pixels. another question: why in mozilla this doesn´t work (it does in Opera 7 and IE6) element.style.left = 50; and how can i solve...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
12
by: Adam Lipscombe | last post by:
Folks, I need to get the contents of a form attribute. In Read/Write mode this is field, so I can use getElementById("name").value In ReadOnly this is just plain text, so I can use...
1
by: George W. | last post by:
Okay, I'm a C#/XML newbie, and I've been wrestling with this for a while now, checked dotnet sites, articles, MSDN Library, etc. and haven't been able to determine why this is happening. I have...
5
by: Don | last post by:
Hi: I have created an xsd from my xml document. I pop this xsd in the following directory: C:\program files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml. That give me...
6
by: trexim | last post by:
Hi, I am trying to create a Web Reference for CSTA using the URL http://www.ecma-international.org/standards/ecma-348/csta-wsdl/csta-wsdl-all-operations.wsdl Visual .Net complains that: "...
5
by: Mike Logan | last post by:
I used WSDL.exe to generate a client side web proxy for a web service, called the web service, got the results but an array returned by the web service is not in the results. However if I use...
4
by: chippy | last post by:
I am finding that when I use the cloneNode method to copy an HTML element that contains a <script> tag, the contents of the <script> tag, (ie. the javascript) are removed. If I do this: var...
6
by: Charleees | last post by:
Hi all, I have a DropDown and a TextBox just bekeow it... I have to get the selected value from dropdown and set it as textBox Text.. The thing is i have to do this Without PostBack..... ...
0
by: rautsmita | last post by:
hello friends , i am using to jdk6 and JAXB2.0, i have geomtry.xsd file i am trying to compile this file using jaxb but i got some error i.e.The particle of the type is not a valid restriction of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
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
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,...

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.