C++ 如果字符串包含数字和字符c++;
本质上,我有一个函数,可以将分数写入文本文件,但是我不确定如何根据分数写入文本文件中的相应行,因此它会写入下一行的文件底部,我还希望有一个函数可以打印前10个分数,但因为该文件不是按分数排序的(如果有一种简单的方法可以做到这一点,那么答案是欢迎的)我的想法是读取文件中的所有行并将它们放入字符串数组中,然后根据数组中的数字对数组进行排序 例如,文本文件的格式为SCORE然后NAME,下面是该文件的一个示例C++ 如果字符串包含数字和字符c++;,c++,arrays,sorting,C++,Arrays,Sorting,本质上,我有一个函数,可以将分数写入文本文件,但是我不确定如何根据分数写入文本文件中的相应行,因此它会写入下一行的文件底部,我还希望有一个函数可以打印前10个分数,但因为该文件不是按分数排序的(如果有一种简单的方法可以做到这一点,那么答案是欢迎的)我的想法是读取文件中的所有行并将它们放入字符串数组中,然后根据数组中的数字对数组进行排序 例如,文本文件的格式为SCORE然后NAME,下面是该文件的一个示例 1548 Bob Jones 604 James Jones 5516 Example Na
1548 Bob Jones
604 James Jones
5516 Example Name
24 Bad Score ikr
要将其打印到文件中,我从用户处获取名称的输入,然后使用
HighScore << totalScore << " " << Name << std::endl;
由于我的想法是使用字符串数组,我仍然不知道如何根据字符串中的初始分数对其进行排序,如果有更好的解决方案从文件中打印出前10名分数,请告诉我!谢谢你,你不能只使用一个大的
字符串,因为604 James Jones在字典上比5516大示例名称。604的第一个字符6大于5516的5,尽管5516的数字大于604
我会使用score
结构的score
而不是string
数组。score
将包含score(可能是int
)和name(astring
),和一个运算符我建议创建一个类来保存分数和名称。然后,您可以为该类的流对象添加运算符和一个比较运算符。运算符您的方法似乎是合法的,只要这样做就行了。当您遇到实际问题时(不仅仅是家庭作业的描述),你可以在这里问。确保你采取更小的步骤,分别解决不同的问题。我的观点是,我不知道如何根据字符串开头的数字排序数组,这就是我要问的xD@meme不要将整行视为一个字符串。正如两个答案所示,使用整数作为分数。同样有趣的是:@Moo是的,这更好。改变代码,但是它更难理解。我认为它是专业人士的一个可爱的把戏,但是它可能太混乱,在实际代码中使用或与学生。是的,我可以解释一下,我是新的编码,更不用说C++,因为我是学生。all@meme添加了一些e解释。不断询问是否不清楚,我会尝试改进答案。如果我也使用您的方法使用文件解析建议,我理解使用iss>>a>>b,但是如果我使用iss>>score>>name,名称字符串是否会考虑空格,或者如果行是240 John Deere,它是否会返回score=240,name=john,然后就什么都没有了?我如何将名称的其余部分包含在空格中,这样它就可以输出score=240,name=john Deere?@meme我忽略了这一点。好眼力。使用iss>>score&&iss.ignore()&&std::getline(iss,name)
ignore
将删除数字后的空格,而getline
将占用iss
的其余部分,因为它正在查找行的结尾,并将首先找到iss
的结尾。我会尝试一下,然后报告结果似乎很完美,谢谢,我将使用两个数组,一个用于分数,一个用于姓名,一个用于d对它们进行排序,无论我对分数数组做什么排序,我都会对名称数组做什么排序,如果我只做一个for循环,打印前10名的分数[i]name[i],那么它应该工作得很好。为此,您需要一个可变长度数组(VLA)(或一个非常大的固定大小数组)因为您不知道文件中有多少个代码> >代码> >条目。VLA:S不受标准C++支持,所以您应该使用<代码> STD::vector < /COD>或<代码> STD::SET<代码>或其他标准容器,将新的<代码>得分>代码>导入。
1) 5516 Example Name
2) 1548 Bob Jones
3) 604 James Jones
4) 24 Bad Score ikr
struct score
{
int mScore;
std::string mName;
bool operator<(const score & other)
{
return mScore < other.mScore;
}
};
#include <algorithm> // std::copy, std::min
#include <iostream> // std::cin, std::cout
#include <iterator> // std::istream_iterator, std::ostream_iterator
#include <set> // std::set
#include <sstream> // std::istringstream
#include <string> // std::string
#include <tuple> // std::tie
class score_t {
public:
bool operator<(const score_t& s) const {
// sort on score in decending order
// if scores are equal, sort on name, ascending order
return std::tie(s.score, name) < std::tie(score, s.name);
}
friend std::istream& operator>>(std::istream& is, score_t& s) {
if(is >> s.score) {
is.ignore(1); // ignore space between score and name
std::getline(is, s.name); // read rest of line
}
return is;
}
friend std::ostream& operator<<(std::ostream& os, const score_t& s) {
return os << s.score << ' ' << s.name;
}
private:
int score{};
std::string name{};
};
int main() {
// test data
std::istringstream cin{
"1548 Bob Jones\n"
"604 James Jones\n"
"5516 Example Name\n"
"100 BBB\n"
"100 AAA\n"
"24 Bad Score ikr\n"};
// Use the new operator>> to read score_t's from a stream and put them in a
// set. std::set has a constructor taking iterators that can be used to populate the
// set directly. Since cin is a stream and not an iterator, we can use
// std::istream_iterator<score_t>(cin) to create an iterator to use as beginning.
// std::istream_iterator<score_t>{} creates an end iterator. Read to the end of file.
std::set<score_t> scores(std::istream_iterator<score_t>(cin),
std::istream_iterator<score_t>{});
// print the top 5 of the collected score_t's
// std::copy can copy a range of values given by iterators and insert them
// where a third iterator points.
// We copy from the beginning and then max 5 score_t's by creating the end
// iterator using std::next. The result is copied to an ostream_iterator
std::copy(scores.cbegin(),
std::next(scores.cbegin(), std::min(scores.size(), 5ul)),
std::ostream_iterator<score_t>(std::cout, "\n"));
}
5516 Example Name
1548 Bob Jones
604 James Jones
100 AAA
100 BBB