C++ C++;字典/列表是否与此Python代码等效?
在我的Python代码中,我有一个问题需要在开始翻译到c++之前澄清:如何制作适当的字典/列表,我可以使用中的“if var In_u2;”的等价物 需要翻译的任意示例:C++ C++;字典/列表是否与此Python代码等效?,c++,visual-studio-2010,windows-7,python-3.2,C++,Visual Studio 2010,Windows 7,Python 3.2,在我的Python代码中,我有一个问题需要在开始翻译到c++之前澄清:如何制作适当的字典/列表,我可以使用中的“if var In_u2;”的等价物 需要翻译的任意示例: CONFIRMATION = ('yes', 'yeah', 'yep', 'yesh', 'sure', 'yeppers', 'yup') DECLINATION = ('no', 'nope', 'too bad', 'nothing') varResponse = str(input('yes or no quest
CONFIRMATION = ('yes', 'yeah', 'yep', 'yesh', 'sure', 'yeppers', 'yup')
DECLINATION = ('no', 'nope', 'too bad', 'nothing')
varResponse = str(input('yes or no question'))
if varResponse in CONFIRMATION:
doSomething()
elif varResponse in DECLINATION:
doSomethingElse()
else:
doAnotherThing()
使用阵列执行类似任务相当容易,例如:
if (userDogName == name[0])
execute something;
但我需要的是:
if (userDogName is one of a population of dog names in a dictionary)
execute something;
您可以使用STL容器类
set
。它使用平衡二叉树:
#include <iostream>
#include <set>
#include <string>
int main(int argc, char* argv[])
{
std::set<std::string> set;
std::set<std::string>::const_iterator iter;
set.insert("yes");
set.insert("yeah");
iter = set.find("yess");
if (iter != set.end( ))
{
std::cout << "Found:" << *iter;
}
else
{
std::cout << "Not found!";
}
return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
std::set;
std::set::const_迭代器iter;
设置。插入(“是”);
设置。插入(“是”);
iter=set.find(“yess”);
if(iter!=set.end())
{
std::cout您可以使用STL容器类set
。它使用平衡二叉树:
#include <iostream>
#include <set>
#include <string>
int main(int argc, char* argv[])
{
std::set<std::string> set;
std::set<std::string>::const_iterator iter;
set.insert("yes");
set.insert("yeah");
iter = set.find("yess");
if (iter != set.end( ))
{
std::cout << "Found:" << *iter;
}
else
{
std::cout << "Not found!";
}
return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
std::set;
std::set::const_迭代器iter;
设置。插入(“是”);
设置。插入(“是”);
iter=set.find(“yess”);
if(iter!=set.end())
{
std::cout这可以通过在任何标准模板库容器上使用std::find
来解决
std::vector<std::string> answers;
std::string input;
...
if(std::find(answers.begin(), answers.end(), input) != answers.end()) {
/* input was found in answers */
} else {
/* input was not found in answers */
}
std::向量答案;
std::字符串输入;
...
if(std::find(answers.begin(),answers.end(),input)!=answers.end()){
/*在答案中找到输入*/
}否则{
/*在答案中找不到输入*/
}
对于较大的列表,最好将列表存储在std::set
对象中,正如Tilo所建议的那样。std::find
也会起到同样的作用。这可以通过在任何标准模板库容器上使用std::find
来解决
std::vector<std::string> answers;
std::string input;
...
if(std::find(answers.begin(), answers.end(), input) != answers.end()) {
/* input was found in answers */
} else {
/* input was not found in answers */
}
std::向量答案;
std::字符串输入;
...
if(std::find(answers.begin(),answers.end(),input)!=answers.end()){
/*在答案中找到输入*/
}否则{
/*在答案中找不到输入*/
}
对于较大的列表,如Tilo所建议的,最好将列表存储在std::set
对象中。std::find
也会起到同样的作用。C++11允许使用与Python代码非常类似的解决方案:
#include <iostream>
#include <set>
#include <string>
using namespace std;
set<string> CONFIRMATION = {"yes", "yeah", "yep", "yesh", "sure", "yeppers", "yup"};
set<string> DECLINATION = {"no", "nope", "too bad", "nothing"};
int main() {
cout << "yes or no question";
string varResponse;
getline(cin, varResponse);
if (CONFIRMATION.find(varResponse) != CONFIRMATION.end()) {
doSomething();
} else if (DECLINATION.find(varResponse) != DECLINATION.end()) {
doSomethingElse();
} else {
doAnotherThing();
}
}
#包括
#包括
#包括
使用名称空间std;
设置确认={“是的”、“是的”、“是的”、“是的”、“当然”、“是的”、“是的”、“是的”};
设置偏差={“否”、“否”、“太差”、“无”};
int main(){
coutC++11允许一个与Python代码非常相似的解决方案:
#include <iostream>
#include <set>
#include <string>
using namespace std;
set<string> CONFIRMATION = {"yes", "yeah", "yep", "yesh", "sure", "yeppers", "yup"};
set<string> DECLINATION = {"no", "nope", "too bad", "nothing"};
int main() {
cout << "yes or no question";
string varResponse;
getline(cin, varResponse);
if (CONFIRMATION.find(varResponse) != CONFIRMATION.end()) {
doSomething();
} else if (DECLINATION.find(varResponse) != DECLINATION.end()) {
doSomethingElse();
} else {
doAnotherThing();
}
}
#包括
#包括
#包括
使用名称空间std;
设置确认={“是的”、“是的”、“是的”、“是的”、“当然”、“是的”、“是的”、“是的”};
设置偏差={“否”、“否”、“太差”、“无”};
int main(){
CUT嗯,C++不适合于小的丢弃程序,因为它不提供很多的底层结构。你的意思是在标准库的顶部创建自己的下一个结构(例如,甚至是简单的集合)。或者使用第三方库,即你的选择。
虽然Python中有电池,但C++没有强大的压力来接受特定的电池(因为没有电池),但是你必须至少选择电池。
对于基本代码,Python代码段
CONFIRMATIONS = ("yes", "yeah", "yep", "yesh", "sure", "yeppers", "yup")
DECLINATIONS = ("no", "nope", "too bad", "nothing")
response = raw_input( "yes or no? " )
if response in CONFIRMATIONS:
pass # doSomething()
elif response in DECLINATIONS:
pass # doSomethingElse()
else:
pass #doAnotherThing()
在C++中可以如下所示:
typedef Set< wstring > Strings;
Strings const confirmations = temp( Strings() )
<< L"yes" << L"yeah" << L"yep" << L"yesh" << L"sure" << L"yeppers" << L"yup";
Strings const declinations = temp( Strings() )
<< L"no" << L"nope" << L"too bad" << L"nothing";
wstring const response = lineFromUser( L"yes or no? " );
if( isIn( confirmations, response ) )
{
// doSomething()
}
else if( isIn( declinations, response ) )
{
// doSomethingElse()
}
else
{
// doAnotherThing()
}
我使用了<代码>操作符> P>,C++不适合于小的跳转程序,因为它不提供很多的底层结构。你的意思是在标准库的顶部创建自己的Trasa结构(比如,甚至是纯集合)。或者使用第三方库,即你的选择。
虽然Python中有电池,但C++没有强大的压力来接受特定的电池(因为没有电池),但是你必须至少选择电池。
对于基本代码,Python代码段
CONFIRMATIONS = ("yes", "yeah", "yep", "yesh", "sure", "yeppers", "yup")
DECLINATIONS = ("no", "nope", "too bad", "nothing")
response = raw_input( "yes or no? " )
if response in CONFIRMATIONS:
pass # doSomething()
elif response in DECLINATIONS:
pass # doSomethingElse()
else:
pass #doAnotherThing()
在C++中可以如下所示:
typedef Set< wstring > Strings;
Strings const confirmations = temp( Strings() )
<< L"yes" << L"yeah" << L"yep" << L"yesh" << L"sure" << L"yeppers" << L"yup";
Strings const declinations = temp( Strings() )
<< L"no" << L"nope" << L"too bad" << L"nothing";
wstring const response = lineFromUser( L"yes or no? " );
if( isIn( confirmations, response ) )
{
// doSomething()
}
else if( isIn( declinations, response ) )
{
// doSomethingElse()
}
else
{
// doAnotherThing()
}
<>我用了一个<代码>操作符,如果列表不是太大,你可以对数组进行线性搜索。我有一些比较大的列表,但是我会考虑小的列表。对于大的列表我应该怎么做?元素的排序是否重要,并且你可以有重复吗?如果列表不是太大,你可以在A上进行线性搜索。雷。我有一些比较大的清单,但我会考虑小的。我该怎么办呢?这些元素的顺序是否重要,你能有重复吗?@阿尔夫:不,我只能使用Calangh。@阿尔夫。不,我只能访问Clang。我喜欢这个方法,但是有个问题。如果我能做什么,我会怎么办?你说过25个术语吗?当然我不会做set.insert(“term”)25次?在C++11中,你可以使用初始值设定项列表。只需搜索std::initializer\u list
就可以理解这个概念。在你的程序中,你只需编写:set as={“a”,“a”…
。在旧版本中,您可能会使用set
的构造函数,该构造函数获取两个指针,并假设这些指针指向数组的开始和结束,然后使用数组初始化集合。但在实际应用程序中,您可能不会硬编码列表,而是从某种配置文件中读取它们?这是我的想法使用insert
的想法。我喜欢这种方法,但有一个问题;如果我说了25个术语,我会怎么做?当然我不会做25次set.insert(“术语”)?在C++11中,你可以使用初始值设定项列表。只需搜索std::initializer\u list
就可以理解这个概念。在你的程序中,你只需写:set as={“a”“A”…
。在旧版本中,您可能会使用set
的构造函数,该构造函数获取两个指针,并假设这些指针指向数组的开始和结束,然后使用数组初始化集合。但在实际应用程序中,您可能不会硬编码列表,而是从某种配置文件中读取它们?这是我的想法使用insert
的背后思想。