Click here to Skip to main content
16,020,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey this is my first post so thanks to anyone that helps.

On to my question! I have a set within a map and the information is being stored properly but not sure how to extract it. My issue is with the if statement at the bottom of my code.

Thanks
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <list>
using namespace std;



//****These are the containers being uses****//
std::map<std::string,set><std::string xmlns:std="#unknown"> > token;
list <string> stack;

//"""""These are the variables that are being used""""//
std::string consumer;
string temp, label;
stringstream ss1;


int main()
{
	//--This is the input of the polish notation
	std::cout << "!!!Hello World!!!" << std::endl<<"Please enter word"<<endl;
	std::cin>> consumer;
	std::cout<<consumer<<std::endl;


	//++This sorts out the word into the map
	for(int i=0; i< (int)consumer.length();i++)
	{
		ss1.str(""); "//clear the stringstream
		ss1<< i+1; //input number of the label into string stream
		label = "s" + (ss1.str()); //add the label number to label "s"
		temp= consumer[i]; //make string character "i" into temp variable
		token[label].insert(temp); //insert the label into map string1 and insert temp (string charater of "i") into set<string> of map
	}

	for (int ie=0; ie<(int)token.size();ie++)
	{
		ss1.str(""); "//clear the stringstream
		ss1<< ie+1;//input number of the label into string stream
		label = "s" + (ss1.str());//add the label number to label "s"
		cout<<label<<endl;
		if (token.find(label) == "+");
		cout<<"addition symbol";

	}


	return 0;
}
</string></string></std::string></list></sstream></set></map></string></vector></iostream>


[Modified: just added two double quotes to color-correct the formatting]
Posted
Updated 27-Dec-10 12:19pm
v2
Comments
William Winner 27-Dec-10 18:22pm    
just a comment...you included using namespace std;

You don't need to keep writing std::cout, std::endl, etc... That's the whole point of the using namespace keyword.
William Winner 27-Dec-10 18:59pm    
From OP:
Yer i know i just started not using namespace std and then decided to half way through

1 solution

First, I don't see how your code would even compile. Your declaration of the map object is a mess. Were you trying to create a map object that takes a string as the first iterator and a set as the second? Then you were you trying to say that you wanted your set to be a string? The way that you've written it, you didn't set a constructor for the set object.

So, let's say that you did want it that way...then your constructor would be:

C++
map<string,set<string>> token;


That would be the first step. But then you have to understand how the map and the set work.

The map lets you assign an object based on a string (in our example). So, a key of "s1" would give you the set for that key. That set would then contain a single string matching the first character in the input.

So, doing
C++
token.find(label)

returns a map object. You would then need to access one of the properties of that map. Using intellisense, you would see that there are two: first and second. First refers to the key. Second to the set object.

So, doing
C++
token.find(label)->second

gives you the set object. Now, you need to get the string in that set. With the set, you would want to use the method begin().

But, that only returns a pointer. So you need to access what is at that pointer.

So, doing
C++
*(token.find(label)->second.begin())

would give you the actual letter that you're looking for.

On a side note, you put a semi-colon at the end of your if statement. That ends the if statement.

I believe what you really want is:
C++
if (*(token.find(label)->second.begin()) == "+")
  cout<<"addition symbol";


Also, creating a map with a set that contains a single string is a bit silly. You could just do it with a string and minimize the headaches.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900