Hi all,
I am doing something terrible wrong, but I dont understand why!
Maybe I am sitting too long in front of this box!
This is a breakdown of some code:
#include <map>
class MyMap : public std::map< double* , int { };
MyMap test;
void findMyDouble(double* d)
{
MyMap::iterator iter;
for( iter = test.begin(); iter != test.end(); ++iter)
{
if(*iter->first == 2.0)
{
//assign t2 to d
d = iter->first;
// d == t2
// *d == 2.0
// okay until here
break;
}
}
}
int main(int argc, char** argv)
{
double* t1 = new double(1.0);
double* t2 = new double(2.0);
double* t3 = new double(3.0);
test.insert(MyMap::value_type(t1,10));
test.insert(MyMap::value_type(t2,11));
test.insert(MyMap::value_type(t3,12));
double* search;
findMyDouble(search);
//search should be:
//search == t2!
//*search == 2.0
//but search is again not defined
MyMap::reverse_iterator iter;
for( iter = test.rbegin(); iter != test.rend(); ++iter)
delete iter->first;
return 0;
}
I need to extract some map-key, which is a pointer.
The key is the pointer-itself, but i have to find a specific pointer-
value.
in the local scope of findMyDouble everything is okay, but in main
nothing happens at all.
Thanks for help for a tired cpp-victim. 5 2744
cppquest wrote:
Hi all,
I am doing something terrible wrong, but I dont understand why!
Maybe I am sitting too long in front of this box!
This is a breakdown of some code:
#include <map>
class MyMap : public std::map< double* , int { };
MyMap test;
void findMyDouble(double* d)
{
MyMap::iterator iter;
for( iter = test.begin(); iter != test.end(); ++iter)
{
if(*iter->first == 2.0)
{
//assign t2 to d
d = iter->first;
// d == t2
// *d == 2.0
// okay until here
break;
}
}
}
int main(int argc, char** argv)
{
double* t1 = new double(1.0);
double* t2 = new double(2.0);
double* t3 = new double(3.0);
test.insert(MyMap::value_type(t1,10));
test.insert(MyMap::value_type(t2,11));
test.insert(MyMap::value_type(t3,12));
double* search;
findMyDouble(search);
//search should be:
//search == t2!
//*search == 2.0
//but search is again not defined
MyMap::reverse_iterator iter;
for( iter = test.rbegin(); iter != test.rend(); ++iter)
delete iter->first;
return 0;
}
I need to extract some map-key, which is a pointer.
The key is the pointer-itself, but i have to find a specific pointer-
value.
in the local scope of findMyDouble everything is okay, but in main
nothing happens at all.
because search is passed by value. findMyDouble gets a *COPY* of
search. You need to pass by reference, or return the found value.
cppquest wrote:
Hi all,
I am doing something terrible wrong, but I dont understand why!
Maybe I am sitting too long in front of this box!
This is a breakdown of some code:
#include <map>
class MyMap : public std::map< double* , int { };
MyMap test;
void findMyDouble(double* d)
{
MyMap::iterator iter;
for( iter = test.begin(); iter != test.end(); ++iter)
{
if(*iter->first == 2.0)
{
//assign t2 to d
d = iter->first;
// d == t2
// *d == 2.0
// okay until here
break;
}
}
}
int main(int argc, char** argv)
{
double* t1 = new double(1.0);
double* t2 = new double(2.0);
double* t3 = new double(3.0);
test.insert(MyMap::value_type(t1,10));
test.insert(MyMap::value_type(t2,11));
test.insert(MyMap::value_type(t3,12));
double* search;
findMyDouble(search);
If you want to modify the value of search by calling findMyDouble, then
findMyDouble needs to take a reference (or pointer) to double*. Declare
it as:
void findMyDouble( double*& d);
//search should be:
//search == t2!
//*search == 2.0
//but search is again not defined
MyMap::reverse_iterator iter;
for( iter = test.rbegin(); iter != test.rend(); ++iter)
delete iter->first;
return 0;
}
I need to extract some map-key, which is a pointer.
The key is the pointer-itself, but i have to find a specific pointer-
value.
in the local scope of findMyDouble everything is okay, but in main
nothing happens at all.
Thanks for help for a tired cpp-victim.
cppquest wrote:
Hi all,
I am doing something terrible wrong, but I dont understand why!
Maybe I am sitting too long in front of this box!
This is a breakdown of some code:
#include <map>
class MyMap : public std::map< double* , int { };
MyMap test;
void findMyDouble(double* d)
{
MyMap::iterator iter;
for( iter = test.begin(); iter != test.end(); ++iter)
{
if(*iter->first == 2.0)
{
//assign t2 to d
d = iter->first;
// d == t2
// *d == 2.0
// okay until here
break;
}
}
}
int main(int argc, char** argv)
{
double* t1 = new double(1.0);
double* t2 = new double(2.0);
double* t3 = new double(3.0);
test.insert(MyMap::value_type(t1,10));
test.insert(MyMap::value_type(t2,11));
test.insert(MyMap::value_type(t3,12));
double* search;
findMyDouble(search);
//search should be:
//search == t2!
//*search == 2.0
//but search is again not defined
MyMap::reverse_iterator iter;
for( iter = test.rbegin(); iter != test.rend(); ++iter)
delete iter->first;
return 0;
}
I need to extract some map-key, which is a pointer.
The key is the pointer-itself, but i have to find a specific pointer-
value.
in the local scope of findMyDouble everything is okay, but in main
nothing happens at all.
Thanks for help for a tired cpp-victim.
search needs to be initialised with a valid address. Either that
or make it a double and call findMyDouble(&search)
JB
cppquest wrote:
Hi all,
I am doing something terrible wrong, but I dont understand why!
Maybe I am sitting too long in front of this box!
This is a breakdown of some code:
[..]
void assign_10_to(int a)
{
a = 10;
}
#include <iostream>
int main()
{
int b = 42;
assign_10_to(b);
std::cout << b;
}
What's this program going to print? Why? Now, let's change it
a little bit
int global_ten = 10;
void assign_10_to(int* a)
{
a = &global_ten;
}
#include <iostream>
int main()
{
int b = 42;
assign_10_to(&b);
std::cout << b;
}
What's this program oging to print? Why? What do you need to
change to make it print '10'?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Feb 22, 10:48 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
cppquest wrote:
Hi all,
I am doing something terrible wrong, but I dont understand why!
Maybe I am sitting too long in front of this box!
This is a breakdown of some code:
[..]
void assign_10_to(int a)
{
a = 10;
}
#include <iostream>
int main()
{
int b = 42;
assign_10_to(b);
std::cout << b;
}
What's this program going to print? Why? Now, let's change it
a little bit
int global_ten = 10;
void assign_10_to(int* a)
{
a = &global_ten;
}
#include <iostream>
int main()
{
int b = 42;
assign_10_to(&b);
std::cout << b;
}
What's this program oging to print? Why? What do you need to
change to make it print '10'?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
To make it short and sweet,
- instead of passing "search" (an uninitialized pointer value with no
storage), pass "&search" (a pointer to the place you expect to find a
pointer on return from findMyDouble)
- modify findMyDouble to expect a pointer to a pointer
- in FindMyDouble, *localSearch = (address of found thing);
Or, as was suggested above, have findMyDouble return the address of
the found double. Then you have "search = findMyDouble()" - much
nicer.
Dana This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Arturo Cuebas |
last post by:
I've got a bunch of file_iterator<> (http://tinyurl.com/3uuxa)
begin/end pairs that point to various chunks in a file. It would be
super-cool, in my program, to be able to treat all of these ranges...
|
by: forester |
last post by:
lets say its common situation when object have subobjects in container
and receives callbacks from contained items. and object want to move
objects in containers on signal(callback).
iterator is...
|
by: TOMERDR |
last post by:
Hi,i am new to stl and i have a question regarding iterators
I have class day contains a map of appointments:
map<time_t,Appointment> m_Appointments;
I would like to write a function...
|
by: wolverine |
last post by:
Hi
I am accessing a map from inside threads. There is a chance
that an element is inserted into the map, from inside any thread. Since
i don't know about thread safety of stl implementation i am...
|
by: toton |
last post by:
Hi,
for my project I needs a few plots to visualize the data (mostly x-y
plot) . Most of the existing plotting library either copy the data and
do plotting, or takes a data pointer to an 2d x-y...
|
by: John |
last post by:
I have a class of my own which has a 2d matrix of type T. I want to
give the users of my library an iterator to iterator thru all the
elements of this matrix ( they dont need to know anything about...
|
by: antani |
last post by:
VolumeType::ISetType::iterator findFirstIteratorMarked(const
VolumeType::ISetType::iterator & it,const
VolumeType::ISetType::iterator & e_it)
{
VolumeType::ISetType::iterator _it=it;
while...
|
by: dkmd_nielsen |
last post by:
I have two rather simple class methods coded in Ruby...my own each
iterator: The iterator is used internally within the class/namespace,
and be available externally. That way I can keep...
|
by: pandit |
last post by:
Hello i dont understand how to Deal with iterator. its little bit
confusing ?
how they work??
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
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...
|
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...
| |