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

Issue using std::map find() method

Hi,
Would you please show me why find doesn't work in my codes below? I always got a segfault.

**************************
Expand|Select|Wrap|Line Numbers
  1. port* macro::get_port(char* myName) {
  2.    printf ("Getting port %s\n",myName);
  3.    port* hey = new port;
  4.    for (_ITER=_PORTS->begin();_ITER!=_PORTS->end();++_ITER) {
  5.       printf("Port %s existed!\n",_ITER->first);
  6.       port* tmp = _ITER->second;
  7.       printf("Port %s has direction %s\n",tmp->get_name(),tmp->get_dir());
  8.    }
  9.  
  10.    if ((_ITER = _PORTS->find(myName)) == _PORTS->end())
  11.       printf("Could not find %s in map\n",myName);
  12.    else {
  13.       _ITER = _PORTS->find(myName);
  14.       printf("Port %s is found\n",_ITER->first);
  15.    }
  16.  
  17.    return (hey);
  18. }
  19.  
*********************

The output is below


Port P2 existed!
Port P2 has direction OUTPUT
Port PA existed!
Port PA has direction OUTPUT
Port P3 existed!
Port P3 has direction INOUT
Could not find P2 in map

*********************
When I test the map in a loop it looks fine. But when I try to find a particular element, it said it couldn't find it. Would you please help me find out the error as I am new to C++ and have spent almost a week on this.


Thank you,
--Binh Pham
Mar 12 '12 #1
1 2015
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. port* hey = new port;
creates a port object but the function doesn't ever use it.
Odd.
But the function returns it anyway. Very odd.

This code:
Expand|Select|Wrap|Line Numbers
  1. for (_ITER=_PORTS->begin();_ITER!=_PORTS->end();++_ITER) {
  2.  5.       printf("Port %s existed!\n",_ITER->first);
  3.  6.       port* tmp = _ITER->second;
  4.  7.       printf("Port %s has direction %s\n",tmp->get_name(),tmp->get_dir());
  5.  8.    }
  6.  
when the port is found and the address put in tmp, the function doesn't return tmp. It just goes ahead as if the loop meant nothing. It returns hey, whihc is never used.

You have a lot of printf statements in the function. Ther should be none. The function should return a vale that represents the condition. The port* should be returned using a port** argument.
Mar 13 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Eric | last post by:
I am certain the answer will be 'NO', but I wanted to ask anyway just incase I have missed something. Everyone knows that one pass data to a page in an anchor tag by using the GET Method: <a...
1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
2
by: Ori :) | last post by:
I am writing an application to retrieve appointments from Outlook, I have the appointment items sorted by "Start" field and now I want to find the first record which's "Start" value is later than...
3
by: Brad | last post by:
I need a routine that runs through the current datatable to check for a duplicate entry using two columns. When I set an array variable to object and then set each variable's data to find, does...
3
by: Tim Frawley | last post by:
I am using the Find method on a dataview. What I have found is that the method appears to be case insensitive. I have tested this and it appears to be true. Is there a way to specify case...
3
by: rnettle | last post by:
Can anyone tell me how to use the DataRowCollection.Find Method on 3 primary keys, 2 of the keys are of the String type and the other is of the DateTime type. I have tried the code below but get...
3
by: muskito.net | last post by:
Hi, I Have the following code: Public Sub UpdateEvent(ByVal ID As Integer, _ ByVal StartTime As Integer, _ ByVal Duration As Integer) EventId = ID Dim EventToUpdate As SchedulerEvent =...
2
by: sianan | last post by:
I am having a problem doing the following in generics. I have two list of a custom item type. I need to iterate through the first list and match each item against another list to see if there is...
0
by: njuneardave | last post by:
This line of code will make the Find method search for a search key in a given range if you search from top to bottom: m_form.richTextBox1.Find(searchKey, searchDownAreaStart, searchDownAreaEnd,...
8
by: Chris Noble | last post by:
I need to check whether a particular user is already a member of an Active Directory Security Group. The following code extract works but only if the user distinguished name is exactly the same...
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
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
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...

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.