[英]Lua tables analogue in C++
I started to learn C++ after many years coding in Lua (the only one language I know).在用 Lua(我唯一知道的一种语言)编码多年后,我开始学习 C++。 Help me please write the same code in C++
请帮助我用 C++ 编写相同的代码
ticker_list="GAZP,SBER,GMKN"
period_muvinga={}
period_muvinga.GAZP=23
period_muvinga.SBER=19
period_muvinga.GMKN=20
for sec in string.gmatch(ticker_list,"%a+") do
local period=period_muvinga[sec]
end
The main question is about 100% analogue of Lua tables in C++.主要问题是大约 100% 模拟 C++ 中的 Lua 表。
Ok, here is the equivalent code written in c++好的,这里是用 c++ 编写的等效代码
#include <iostream>
#include <map>
#include <string>
int main ()
{
std::map<std::string, int> period_muvinga;
period_muvinga["GAZP"] = 23;
period_muvinga["SBER"] = 19;
period_muvinga["GMKN"] = 20;
// Iterate over all the key,value pairs in map
//
for (const auto & p: period_muvinga)
{
// Extract key from pair
std::string ticker = p.first;
// Extract value from pair
int period = p.second;
std::cout << ticker << " = " << period << std::endl;
}
return 0;
}
As others have said, std::map
is not exactly the same as lua tables.正如其他人所说,
std::map
与 lua 表并不完全相同。 So depending on what you want to do, std::map
may or may not fit your needs因此,根据您想要做什么,
std::map
可能适合也可能不适合您的需求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.