473,407 Members | 2,326 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,407 software developers and data experts.

Are new std::map elements initialized?

Hi!

Given the following code:

#include <map>
#include <string>
#include <cassert>

int main()
{
std::map< std::string, unsigned > my_map;
++my_map["Test"];

assert( my_map["Test"] == 1 );
}

Can I rely on the fact that the newly created map element with key
"Test" will be created with a value of 0?

Josuttis's "The C++ Standard Library" seems to indicate so (pg. 207).

The Standard briefly states (23.3.1.2 para 1):

T& operator[] (const key_type& x);

Returns: (*((insert(make_pair(x, T()))).first)).second

So it looks like the default constructor for T is called. For PODs like
"unsigned", does the default constructor (?) guarantee
0-initialization?

Thanks for your insights!

Cheers,
Andre

Nov 22 '05 #1
4 4197
As I know there is no such thing as default constructor for C/C++ basic
types.

Try

void foo()
{
int i, j; // no default constructor
}

Neither i nor j are assigned 0. They both are uninitialized (junk).

Gregory

Nov 22 '05 #2
Gregory wrote:
As I know there is no such thing as default constructor for C/C++ basic
types.

Try

void foo()
{
int i, j; // no default constructor
}

Neither i nor j are assigned 0. They both are uninitialized (junk).

Gregory


but i = int() does initialize to 0.
So, If a map uses:
return (*((insert(make_pair(x, T()))).first)).second
as Josuttis claims, then if T is int, then the make_pair() call with
int() as the second parameter creates a pair with 0 as the second element.
Nov 22 '05 #3
Yes, you are right. I wrote a test and my_map["Test"] returned 0.
I also checked g++ STL implementation we have and indeed
map::operator[] calls integer constructor - int() as you said.

_Tp& operator[](const key_type& __k) {
iterator __i = lower_bound(__k);
// __i->first is greater than or equivalent to __k.
if (__i == end() || key_comp()(__k, (*__i).first))
__i = insert(__i, value_type(__k, _Tp()));
return (*__i).second;
}
Gregory

Nov 22 '05 #4
in*****@gmail.com wrote:
Hi!

Given the following code:

#include <map>
#include <string>
#include <cassert>

int main()
{
std::map< std::string, unsigned > my_map;
++my_map["Test"];

assert( my_map["Test"] == 1 );
}

Can I rely on the fact that the newly created map element with key
"Test" will be created with a value of 0?


Yes. std::map::operator[] returns (*((insert(make_pair(x,
T()))).first)).second and T() default-initializes an object. For
non-PODs, the default ctor is called; for arrays, each element is
default-initialized; other types are zero-initialized.
Jonathan
Jonathan

Nov 22 '05 #5

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

Similar topics

5
by: EnTn | last post by:
Hi Everyone... I've been trying to use a std::map to do some storage. Basically, i'm storing double values using a Key Object. The Key object is quite simple and is just a pair of int's...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
2
by: Gernot Frisch | last post by:
Hi, I know I can give a std::map a sorting function in the c'tor. However, I would need to _change_ the sorting behaviour of an existing instance. How would I do that? Thank you, --
5
by: Dilip | last post by:
Hi Folks I know the C++ standard doesn't talk about threads. However I am a little bit curious as to what might or might not happen with a particular scenario I encountered in my project. Its...
3
by: tezheng | last post by:
since std::map is some kind of std::set<std::pair> can i use "set_difference" with std::map? how to deal with the "operator =" of std::pair<T1, T2>, as T1 and T2 could be various type.
8
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
6
by: brzrkr0 | last post by:
A portion of my program needs to initialize a std::set<intto have all the keys in a std::map<int, double>. The code I've pasted below works, but I'm wondering if there's a more elegant way to do...
6
by: Juha Nieminen | last post by:
joseph cook wrote: Not always. By default, yes, but you can specify other comparators, eg: std::map<int, int, std::greaterreversedMap;
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.