从文本文件中读取输入并将其提取 - C++
Reading input from a text file and extracting it - C++
我正在尝试逐行读取文本文件并将数据提取到我的程序中。 本质上,C++程序将读取已经制作的输入文件,该文件采用以下格式:
Republican Senator John McMahon
Democrat Mayor Steven Markel
Republican Judge Matt Stevens
Democrat Senator Anthony Quizitano
S R
M D
J R
...
..
..
格式基本上是前 3 行包括政党、职位和姓名,接下来的行表示"结果",其形式为:
[第一封立场信] [政党投票赞成]
因此,例如,如果您看到S R,这意味着参议员获得1票,然后投票给共和党候选人。
这是我到目前为止所拥有的:
#include<fstream>
int main()
{
std::ifstream input("file.txt");
}
我的理解是,这将允许我输入文件,并逐行浏览它,但我不确定我应该如何从这里开始实现这一目标......有什么帮助吗?
谢谢!
为了好玩和荣耀,这是一个基于Boost Spirit的实现。我添加了更多虚假的投票输入,只是为了可以显示一些东西。
-
我不确定候选人和选票之间是否存在1:1的关系(我不是美国公民,我不知道列出的候选人是否会投票或被投票)。所以我决定只使用假数据。
const std::string input = "Republican Senator John McMahonn" "Democrat Senator Anthony Quizitanon" "S Rn" "S Rn" "S Rn" "Democrat Mayor Steven Markeln" "Republican Judge Matt Stevensn" "M Dn" "J Rn" "S Rn" "S Rn";
但是,该代码可以用于这两个目的。
- 我使输入以什么顺序出现并不重要。
- 但是,您可以选择断言单个字母 (S,M,J) 实际上对应于该点之前列出的位置。通过取消注释检查
posletter_check
来启用此功能
在 http://liveworkspace.org/code/d9e39c19674fbf7b2419ff88a642dc38 上观看现场演示
#define BOOST_SPIRIT_USE_PHOENIX_V3
#define BOOST_RESULT_OF_USE_DECLTYPE
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iomanip>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
struct Candidate { std::string party, position, name; };
BOOST_FUSION_ADAPT_STRUCT(Candidate, (std::string, party)(std::string, position)(std::string, name))
typedef std::map<std::pair<char, char>, size_t> Votes;
typedef std::vector<Candidate> Candidates;
template <typename It>
struct parser : qi::grammar<It>
{
mutable Votes _votes;
mutable Candidates _candidates;
parser() : parser::base_type(start)
{
using namespace qi;
using phx::bind; using phx::ref; using phx::val;
start = (line % eol) >> *eol >> eoi;
line =
vote [ phx::bind(&parser::register_vote, phx::ref(*this), _1) ]
| candidate [ phx::push_back(phx::ref(_candidates), _1) ]
;
vote %= graph
// Comment the following line to accept any single
// letter, even if a matching position wasn't seen
// before:
[ _pass = phx::bind(&parser::posletter_check, phx::ref(*this), _1) ]
>> ' '
>> char_("RD")
;
candidate = (string("Republican") | string("Democrat"))
>> ' '
>> as_string [ +graph ]
>> ' '
>> as_string [ +(char_ - eol) ]
;
}
private:
bool posletter_check(char posletter) const
{
for (auto& c : _candidates)
if (posletter == c.position[0])
return true;
return false;
}
void register_vote(Votes::key_type const& key) const
{
auto it = _votes.find(key);
if (_votes.end()==it)
_votes[key] = 1;
else
it->second++;
}
qi::rule<It, Votes::key_type()> vote;
qi::rule<It, Candidate()> candidate;
qi::rule<It> start, line;
};
int main()
{
const std::string input =
"Republican Senator John McMahonn"
"Democrat Senator Anthony Quizitanon"
"S Rn"
"S Rn"
"S Rn"
"Democrat Mayor Steven Markeln"
"Republican Judge Matt Stevensn"
"M Dn"
"J Rn"
"S Rn"
"S Rn";
std::string::const_iterator f(std::begin(input)), l(std::end(input));
parser<std::string::const_iterator> p;
try
{
bool ok = qi::parse(f,l,p);
if (ok)
{
std::cout << "ncandidate listn";
std::cout << "------------------------------------------------n";
for (auto& c : p._candidates)
std::cout << std::setw(20) << c.name << " (" << c.position << " for the " << c.party << "s)n";
std::cout << "nVote distribution:n";
std::cout << "------------------------------------------------n";
for (auto& v : p._votes)
std::cout << '(' << v.first.first << "," << v.first.second << "): " << v.second << " votes " << std::string(v.second, '*') << "n";
}
else std::cerr << "parse failed: '" << std::string(f,l) << "'n";
if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'n";
} catch(const qi::expectation_failure<std::string::const_iterator>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'n";
}
}
输出:
candidate list
------------------------------------------------
John McMahon (Senator for the Republicans)
Anthony Quizitano (Senator for the Democrats)
Steven Markel (Mayor for the Democrats)
Matt Stevens (Judge for the Republicans)
Vote distribution:
------------------------------------------------
(J,R): 1 votes *
(M,D): 1 votes *
(S,R): 5 votes *****
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 无法从 std::string 中提取C++ Unicode 符号
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何从适合一定范围的数字中提取数字?
- C++从通用引用中提取实际类型
- 从运行服务的应用程序代码中提取窗口句柄
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- std::unordered_map::提取引用/指针失效