C ++映射和设置问题 [英] C++ map and set issue

查看:95
本文介绍了C ++映射和设置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,这是我的第一篇文章,感谢所有提供帮助的人.

关于我的问题!我在地图中有一个设置,信息已正确存储,但不确定如何提取它.我的问题是代码底部的if语句.

谢谢

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]

推荐答案

首先,我不知道您的代码将如何编译.您对地图对象的声明一团糟.您是否要创建一个将字符串作为第一个迭代器并将一个集合作为第二个迭代器的地图对象?然后,您试图说要让您的集合成为字符串吗?编写方式,您没有为set对象设置构造函数.

所以,假设您确实希望那样……那么您的构造函数将是:

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:

map<string,set<string>> token;



那将是第一步.但是随后您必须了解地图和集合的工作方式.

该映射使您可以基于字符串分配对象(在我们的示例中).因此,键"s1"将为您提供该键的设置.然后,该集合将包含与输入中的第一个字符匹配的单个字符串.

所以,做



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

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

token.find(label)->second


给您设置的对象.现在,您需要获取该集合中的字符串.有了该集合,您将希望使用方法begin().

但是,那只会返回一个指针.因此,您需要访问该指针处的内容.

所以,做


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

*(token.find(label)->second.begin())


会给您您要查找的实际字母.

附带说明一下,您在if语句的末尾添加了分号.结束if语句.

我相信您真正想要的是:


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:

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.


这篇关于C ++映射和设置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆