如何解析 char 指针字符串并将其特定部分放入 C++ 的映射中?
How can I parse a char pointer string and put specific parts it in a map in C++?
假设我有一个这样的字符指针:
const char* myS = "John 25 Lost Angeles";
我想解析这个字符串并将其放入哈希图中,这样我就可以只根据他的名字检索这个人的年龄和城市。示例:
std::map<string, string> myMap;
john_info = myMap.find("John");
我怎样才能以一种优雅的方式对 return John 的所有信息执行此操作?我来自 Java 背景,我真的很想知道这在 C++ 中是如何正确完成的。如果你能告诉我如何使用提升图来做到这一点,那也很酷(如果那样更容易的话)。谢谢。
我将向您展示一种使用 Boost 的方法:
#include <map>
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
using Name = std::string;
struct Details {
unsigned age;
std::string city;
};
using Map = std::map<Name, Details>;
using Entry = Map::value_type;
BOOST_FUSION_ADAPT_STRUCT(Details, age, city)
int main() {
Map persons;
std::string_view myS = //
"John 25 Lost Angeles\n"
"Agnes 22 Minion Appolis";
auto name = x3::lexeme[+x3::graph];
auto age = x3::uint_;
auto city = x3::raw[*(x3::char_ - x3::eol)];
auto details = x3::rule<struct details_, Details>{} = age >> city;
auto line = name >> details;
auto grammar = x3::skip(x3::blank)[line % x3::eol];
if (x3::parse(myS.begin(), myS.end(), grammar, persons)) {
for (auto& [name, details] : persons)
std::cout << name << " has age " << details.age << "\n";
for (auto& [name, details] : persons)
std::cout << name << " lives in " << details.city << "\n";
}
// lookup:
std::cout << "John was " << persons.at("John").age << " years old at the time of writing\n";
}
版画
Agnes has age 22
John has age 25
Agnes lives in Minion Appolis
John lives in Lost Angeles
John was 25 years old at the time of writing
要使用 hash-map,只需替换
using Map = std::map<Name, Details>;
和
using Map = std::unordered_map<Name, Details>;
现在输出将按 implementation-defined 顺序排列。
警告
如果这是 home-work,请不要使用这种(某种)方法。很明显它是 copy-pasted。永远不要使用你不完全理解的代码。
假设我有一个这样的字符指针:
const char* myS = "John 25 Lost Angeles";
我想解析这个字符串并将其放入哈希图中,这样我就可以只根据他的名字检索这个人的年龄和城市。示例:
std::map<string, string> myMap;
john_info = myMap.find("John");
我怎样才能以一种优雅的方式对 return John 的所有信息执行此操作?我来自 Java 背景,我真的很想知道这在 C++ 中是如何正确完成的。如果你能告诉我如何使用提升图来做到这一点,那也很酷(如果那样更容易的话)。谢谢。
我将向您展示一种使用 Boost 的方法:
#include <map>
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
using Name = std::string;
struct Details {
unsigned age;
std::string city;
};
using Map = std::map<Name, Details>;
using Entry = Map::value_type;
BOOST_FUSION_ADAPT_STRUCT(Details, age, city)
int main() {
Map persons;
std::string_view myS = //
"John 25 Lost Angeles\n"
"Agnes 22 Minion Appolis";
auto name = x3::lexeme[+x3::graph];
auto age = x3::uint_;
auto city = x3::raw[*(x3::char_ - x3::eol)];
auto details = x3::rule<struct details_, Details>{} = age >> city;
auto line = name >> details;
auto grammar = x3::skip(x3::blank)[line % x3::eol];
if (x3::parse(myS.begin(), myS.end(), grammar, persons)) {
for (auto& [name, details] : persons)
std::cout << name << " has age " << details.age << "\n";
for (auto& [name, details] : persons)
std::cout << name << " lives in " << details.city << "\n";
}
// lookup:
std::cout << "John was " << persons.at("John").age << " years old at the time of writing\n";
}
版画
Agnes has age 22
John has age 25
Agnes lives in Minion Appolis
John lives in Lost Angeles
John was 25 years old at the time of writing
要使用 hash-map,只需替换
using Map = std::map<Name, Details>;
和
using Map = std::unordered_map<Name, Details>;
现在输出将按 implementation-defined 顺序排列。
警告
如果这是 home-work,请不要使用这种(某种)方法。很明显它是 copy-pasted。永远不要使用你不完全理解的代码。