LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 12-13-2010, 06:07 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
How to insert data in a nested STL map ?


Following is the declaration:
Code:
std :: map <QString, std :: vector <std :: pair <QString, QString> > > configFileDataVector;
How should I insert data in it?
All the examples which I have looked up in Google are of plain maps!!

I need help!

I tried this, but it failed:
Code:
configFileDataVector.insert (pair <QString, std :: vector <std :: pair <QString, QString> > > ("df", ("1","4")));
and also tried this:
Code:
configFileDataVector.insert (std::make_pair ("fdsf", ("Peter Q.","5328")));
It shows a warning:
Quote:
left-hand operand of comma has no effect

Last edited by Aquarius_Girl; 12-13-2010 at 06:26 AM.
 
Old 12-13-2010, 12:54 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
There's no point getting tricky with this, it just makes for confusing syntax when you try:
Code:
configFileDataVector["fdsf"].push_back(std::pair<QString, QString>("Peter Q.","5328"));
 
1 members found this post helpful.
Old 12-15-2010, 02:23 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks for bothering

The following made it work and you also wrote the similar thing, and I admit that I didn't put much brains into it, I shall think harder next time!

I have shown the relevant code here, it might be helpful to someone else.
Code:
std :: map <QString, std :: vector <std :: pair <QString, QString> > > configFileDataVector;
std :: map <QString, std :: vector <std :: pair <QString, QString> > > :: iterator mapIterator;

for (mapIterator = configFileDataVector.begin(); mapIterator != configFileDataVector.end(); ++mapIterator)
{
   if ((*mapIterator).first == openingTagName)
   {
      configFileDataVector[openingTagName].push_back (std :: make_pair (objQXmlReader.name().toString(), objQXmlReader.readElementText ()));
      break;
    }
}

Last edited by Aquarius_Girl; 12-15-2010 at 02:32 AM.
 
Old 03-02-2011, 01:43 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
I am not able to figure out these simple issues on my own Feel free to throw bricks and bats at me!

The map has been declared as:
Code:
std :: map <unsigned int, char[4111]> dataReceivedFromComponents;
I am filling in the data as:
Code:
char temp [4111];
memcpy (temp, receivedData, (dataSize + 16));
dataReceivedFromComponents [0] = temp;
and getting the error:
Quote:
error: invalid array assignment
HELP!
 
Old 03-02-2011, 04:15 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I don't have much time to explain, but take a look at the following; it will probably simplify your code:
Code:
#include <string>
#include <map>

using namespace std;

int main()
{
   typedef basic_string<char> ComponentData;
   map<int, ComponentData>    dataRcvdFromComponent;

   char   receivedData[4011] = {0};  // data from your component; for now, all zeroes
   size_t dataSize = 3456 + 16;      // number of bytes received from the component; not sure where the 16 comes from?

   dataRcvdFromComponent.insert(make_pair(0, ComponentData(receivedData, dataSize)));

   // or the less efficient, but easier to read

   dataRcvdFromComponent[0] = ComponentData(receivedData, dataSize);
}
--------------
EDIT:

Now that I have the time, what I wanted to indicate earlier is that the "value" type of your STL map needs to be copyable, and that a char array does not offer that feature. In other words, in regular C or C++, this would not work:
Code:
char a[4111];
char b[4111];

b = a;   // error: ISO C++ forbids assignment of arrays
P.S. If your component data is a collection of unsigned bytes, then use "unsigned char" in lieu of "char" above; or better yet, use "uint8_t".

Last edited by dwhitney67; 03-02-2011 at 07:07 AM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Data structures using STL vbx_wx Programming 4 05-20-2010 10:01 AM
c++, linux, Do STL map iterator always return in same order when iterate from begin( linux_linux Linux - Newbie 7 03-15-2008 08:56 PM
stl map reverse iterator r.stiltskin Programming 0 03-05-2007 11:06 AM
STL Map problem krishnacins Programming 4 04-20-2006 12:23 AM
problem with using STL set and its funciton erase , insert ashwinipahuja Programming 5 06-02-2004 12:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:59 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration