LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-07-2012, 11:41 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 386

Rep: Reputation: 32
Arrays using STD containers


Hi, majority of examples of the containers such as std::map are based on a key value pair. Say for example if I want to have something similar to an array, how do I do this? An example would be like the following.

Key 1, Value 1a, Value 1b
Key 2, Value 2a, Value 2b
Key 3, Value 3a, Value 3c

An example application would be using the keys to store product codes, value <x>a to store the price, and value <x>b to store availability (true or false as a boolean). How do I declare the variable in this case?
 
Old 02-07-2012, 12:27 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Ubuntu
Posts: 1,160

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Originally Posted by grob115 View Post
Hi, majority of examples of the containers such as std::map are based on a key value pair. Say for example if I want to have something similar to an array, how do I do this? An example would be like the following.

Key 1, Value 1a, Value 1b
Key 2, Value 2a, Value 2b
Key 3, Value 3a, Value 3c

An example application would be using the keys to store product codes, value <x>a to store the price, and value <x>b to store availability (true or false as a boolean). How do I declare the variable in this case?
Define a structure to contain the values; then use this type as the value-type for the std::map. Here's a simple example:
Code:
#include <map>
#include <string>
#include <iostream>
#include <algorithm>

struct Values
{
    Values(const int v1 = 0, const int v2 = 0) : val1(v1), val2(v2) {}

    int val1;
    int val2;

    friend std::ostream& operator<<(std::ostream& os, const Values& v)
    {
        os << v.val1 << ", " << v.val2;
        return os;
    }
};

void displayEntry(const std::pair<int, Values>& entry)
{
   std::cout << "Entry " << entry.first << " has these values: " << entry.second << std::endl;
}

int main()
{
    typedef std::map<int, Values> MyMap;

    MyMap mm;

    mm[20] = Values(20, 200);   // note: copy-constructor called here!  This may not be desirable.
    mm[10] = Values(10, 100);   // same here
    mm[30] = Values(30, 300);   // same here

   std::for_each(mm.begin(), mm.end(), displayEntry);
}

Last edited by dwhitney67; 02-07-2012 at 12:29 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template algorithm for std::vector and std::string CartmanYO Programming 4 09-08-2010 09:56 AM
c++ containers vendtagain Programming 6 12-22-2009 04:28 PM
SUNWs8brandk and containers thllgo Solaris / OpenSolaris 9 04-03-2009 03:23 PM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM


All times are GMT -5. The time now is 02:55 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration