Another map question

This is a discussion on Another map question within the C++ Programming forums, part of the General Programming Boards category; I'm working on a simple data structure and some functions that take various shapes and perform calculations on them. The ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    Bakersfield, California
    Posts
    22

    Question Another map question

    I'm working on a simple data structure and some functions that take various shapes and perform calculations on them. The shapes will be constructed out of real physical materials, and I need a way to count each segment length. The count and lengths are arbitrary, as are the number of lengths.

    I know this code isn't complete, I'm just showing an outline of what I'm trying to accomplish.

    Code:
    #include <map>
    #include <vector>
    
    using std::map;
    using std::vector;
    
    struct SEG_PROPERTIES
    	{
    	int material;
    	unsigned int thickness;
    	bool is_coated;
    	};
    
    struct SEG_COUNTS
    	{
    	struct SEG_PROPERTIES segProperties;
    
    	// For total length of all segments with same properties
    	unsigned int totalLength;
    
    	// first = length of segment
    	// second = count of segments with length
    	map <unsigned int, unsigned int> segment;
    	};
    
    vector <SEG_COUNTS> segCounts;
    
    void someFunction(void)
    	{
    	// Misc code...
    
    	// Line in question
    	segCounts[1].segment.first = 5;
    	}
    When I try to refer to the .first or .second elements of segCounts[].segment I get this compilation error:
    Code:
    error C2039: 'first' : is not a member of std::map<_Kty,_Ty>
    So I thought that maybe for some reason I need to specify using a pair in the declaration of segment...

    Code:
    #include <utility>
    using std::pair;
    
    ...
    
    struct SEG_COUNTS
    	{
    ...
    
    	map <pair<unsigned int, unsigned int> > segment;
    	};
    But that gives me different errors, and I really don't think that's the way I'm supposed to declare a map. It's obvious I'm doing something wrong, maybe the syntax is eluding me. Anyone care to enlighten me?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    segCounts[1].segment is a std::map, and a std::map does not have a public member named first (or second). What exactly were you trying to do on that line?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,604
    first is part of std::pair which map::find() returns. You are using the map incorrectly.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    Bakersfield, California
    Posts
    22
    What I'm really looking to do is retrieve and set the two values in segment. I thought that this was done with the .first and .second members of the map, but I guess I'm mistaken.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    Perhaps you are looking to use a pair instead of a map?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    Bakersfield, California
    Posts
    22
    I need a map, I think. For each segCounts there will be an assortment of length/count pairs that need to be stored.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    hmm... okay, in your original code, you need to understand that segCounts[1].segment is a map. You might write segCounts[1].segment[5], which would then be an unsigned int presumably representing the count of segments with length 5.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Bakersfield, California
    Posts
    22
    Okay, I think I've got it. I just needed to get an iterator to the segment in question...

    Code:
    void someFunction(void)
    	{
    	map <unsigned int, unsigned int>::iterator iterCurrent;
    	unsigned int length = 4;
    	unsigned int count = 1;
    
    	iterCurrent = segCounts[1].segment.find(length);
    	iterCurrent->second = count;
    
    	}

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,604
    ...which is what I said in my post.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    Bakersfield, California
    Posts
    22
    Yes, thank you Bubba. When I first read your post I didn't quite understand, I'd never used the ::find member function before. You definitely got me on track there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. another map question
    By m37h0d in forum C++ Programming
    Replies: 5
    Last Post: 01-26-2009, 09:25 AM
  3. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  4. New editor updates
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-05-2005, 02:26 PM
  5. Keys of a map
    By nickname_changed in forum C++ Programming
    Replies: 4
    Last Post: 07-10-2003, 04:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21