hi there,
I would like to know if the following piece of code is garantee to
work. I am afraid that the cstring address I am using in the std::map
found from a request in std::set is not garantee to remain the same as
the std::set grows...
thanks
-Mathieu
#include <set>
#include <map>
#include <iostream>
struct FileValuePair {
const char *filename;
const char *value;
};
static FileValuePair MAPPINGS[] = {
{ "foo1.dat" , "value1" },
{ "foo2.dat" , "value2" },
{ "foo3.dat" , "value1" },
{ "foo4.dat" , "value3" },
{ "foo5.dat" , "value2" },
{ "foo6.dat" , "value3" },
{ NULL , NULL },
};
int main()
{
FileValuePair *p = MAPPINGS;
std::set< std::string values;
std::map< const char *, const char * mappings;
while(p->filename)
{
values.insert( p->value );
// find back the address:
const char *v = values.find( p->value )->c_str();
mappings.insert(
std::map<const char*,const char*>::value_type(p->filename, v));
++p;
}
std::map<const char*,const char*>::const_iterator it =
mappings.begin();
for(; it != mappings.end(); ++it)
{
std::cout << it->first << " -" << it->second << std::endl;
}
return 0;
} 2 3056
mathieu wrote:
I would like to know if the following piece of code is garantee to
work. I am afraid that the cstring address I am using in the std::map
found from a request in std::set is not garantee to remain the same as
the std::set grows...
Insertions in 'std::set' or 'std::map' do not invalidate iterators
or references. The call to 'find' returns an iterator. You call the
'c_str()' for the object referred to by the iterator. The object is
not going to change unless you remove the entry itself from the set.
So, the pointer returned by 'c_str()' should still be valid up until
the set is destroyed.
At least that's my take on it...
>
thanks
-Mathieu
#include <set>
#include <map>
#include <iostream>
struct FileValuePair {
const char *filename;
const char *value;
};
static FileValuePair MAPPINGS[] = {
{ "foo1.dat" , "value1" },
{ "foo2.dat" , "value2" },
{ "foo3.dat" , "value1" },
{ "foo4.dat" , "value3" },
{ "foo5.dat" , "value2" },
{ "foo6.dat" , "value3" },
{ NULL , NULL },
};
int main()
{
FileValuePair *p = MAPPINGS;
std::set< std::string values;
std::map< const char *, const char * mappings;
while(p->filename)
{
values.insert( p->value );
// find back the address:
const char *v = values.find( p->value )->c_str();
mappings.insert(
std::map<const char*,const char*>::value_type(p->filename, v));
++p;
}
std::map<const char*,const char*>::const_iterator it =
mappings.begin();
for(; it != mappings.end(); ++it)
{
std::cout << it->first << " -" << it->second << std::endl;
}
return 0;
}
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
In article <219f47cb-04a3-4d05-9d82- 4e**********@i29g2000prf.googlegroups.com>, ma***************@gmail.com
says...
hi there,
I would like to know if the following piece of code is garantee to
work. I am afraid that the cstring address I am using in the std::map
found from a request in std::set is not garantee to remain the same as
the std::set grows...
It does rebalance, but rebalancing only involves changing the
arrangement of the nodes in the tree. Each node contains pointers to one
or two children. Rebalancing involves changing the pointers between
nodes, but does not actually move the nodes themselves. Addresses and/or
iterators that refer to objects you store in the set/map are never
invalidated by inserting into the set/map.
--
Later,
Jerry.
The universe is a figment of its own imagination. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ma740988 |
last post by:
The position returned via the STL std::set container never made much
sense to me. When you insert elements within the container, the
position returned - via find - does not reflect the actual...
|
by: Jason Heyes |
last post by:
I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.
|
by: snnn |
last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as
iterator set::begin() const.
However, why should a const object returns a non-const iterator?...
|
by: Peter Jansson |
last post by:
Hello,
I have the following code:
std::map<int,std::set<std::string> > k;
k="1234567890";
k="2345678901";
//...
std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
|
by: jimmy |
last post by:
Hi,
I am getting a Fatal signal with std::set::insert(). I'm really not
sure what to
try next:
Here is my code:
std::cout << "max size: " << _indices.max_size() << std::endl;
std::cout <<...
|
by: danibe |
last post by:
I never had any problems storing pointers in STL
containers such std::vector and std::map. The benefit
of storing pointers instead of the objects themselves
is mainly saving memory resources and...
|
by: Cory Nelson |
last post by:
Does anyone know how std::set prevents duplicates using only std::less?
I've tried looking through a couple of the STL implementations and
their code is pretty unreadable (to allow for different...
|
by: shuisheng |
last post by:
Dear All,
std::set is sorted. So I am wondering is there any fast way to access
(sucn as random access) to its elements just like std::vector.
Assume I have a set
std::set<inta;
So I can...
|
by: Renzr |
last post by:
I have a problem about the std::set<>iterator. After finding a term in
the std::set<>, i want to know the distance from
the current term to the begin(). But i have got a error. Please offer
me...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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: 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,...
|
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...
| |