以下对于初学者来说可能有点复杂,但既然我们在谈论 C++,我们也应该寻找一种“更”面向目标的方法。
您设计了一个名为 bio 的课程。在面向对象的语言中,您将把对象的所有数据以及对这些数据进行操作的所有函数都放在类中。所以你需要添加成员函数。这个想法是将所有数据封装在一个对象中。外界应该对上课的细节一无所知。您只需通过成员函数访问它。如果您想稍后进行更改,您将在类的成员函数中执行此操作。程序的其余部分将继续工作。
此外,我们绝对应该使用 C++ 语言功能。例如,您应该将 std::string 用于字符串,而不是普通的旧 C 样式字符数组。您基本上不应该在 C++ 中使用 C 样式数组。请改用 STL 容器。
那么,让我们设计一个包含数据成员和成员函数的类。由于目前我们只需要输入和输出功能,因此我们覆盖了插入器和提取器操作符。这些操作员知道类的数据和行为,并且会小心。
查看以下程序:
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <sstream>
struct Bio
{
// Data
unsigned int id{};
std::string name{};
std::string address{};
unsigned int age{};
// Overload extractor operator to read all data
friend std::istream& operator >> (std::istream& is, Bio& b) {
std::string textLine{};
if (std::getline(is, textLine)) {
std::istringstream textLineStream{textLine};
textLineStream >> b.id >> b.name >> b.address >> b.age;
}
return is;
}
// Overload inserter operator to print the data
friend std::ostream& operator << (std::ostream& os, const Bio& b) {
return os << b.id << " " << b.name << " " << b.address << " " << b.age;
}
};
std::istringstream sourceFile{R"(1 John Address1 31
2 Paul Address2 32
3 Ringo Address3 33
4 George Address4 34
)"};
int main()
{
// Define Variable and read complete source file
std::vector<Bio> bio{std::istream_iterator<Bio>(sourceFile), std::istream_iterator<Bio>()};
// Sort the guys by name
std::sort(bio.begin(), bio.end(), [](const Bio& b1, const Bio& b2){ return b1.name < b2.name;});
// Show output on screen
std::copy(bio.begin(),bio.end(),std::ostream_iterator<Bio>(std::cout, "\n"));
return 0;
}
一些cmets。在 StackOverflow 上,我无法使用文件。所以在我的示例程序中,我改用std::istringstream。但这也是std::istream。您也可以使用任何其他std::istream。所以如果你定义了一个```std::ifstreamto read from a file, then it will work in the same way as thestd::istringstream```。
请看。提取器操作符完成读取源文件的全部工作。它是封装的。没有外部函数需要知道它是如何工作的。
在 main 函数中,我们定义了一个 std::vector 并使用它的范围构造函数来指定数据的来源。我们给它std::istream_iterator,它会遍历输入数据并调用提取器操作符,直到读取所有内容。
然后我们按名称排序并将结果复制到输出。
您可能会注意到输入数据中的字段以空格分隔。这通常不适用于非原子数据。名称可以由两部分组成,地址可以包含街道和城市。为此,我们发明了 CSV(逗号分隔值)文件。
请看下面更真实的灵魂。
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <sstream>
#include <regex>
struct Bio
{
// Data
unsigned int id{};
std::string name{};
std::string address{};
unsigned int age{};
// Overload extractor operator to read all data
friend std::istream& operator >> (std::istream& is, Bio& b) {
std::string line{};
std::regex re{";"};
if (std::getline(is, line)) {
std::vector<std::string> token{std::sregex_token_iterator(line.begin(), line.end(), re, -1), std::sregex_token_iterator()};
if (4 == token.size()) {
b.id = std::stoul(token[0]);
b.name = token[1];
b.address = token[2];
b.age = std::stoul(token[3]);
}
}
return is;
}
// Overload inserter operator to print the data
friend std::ostream& operator << (std::ostream& os, const Bio& b) {
return os << b.id << ", " << b.name << ", " << b.address << ", " << b.age;
}
};
std::istringstream sourceFile{R"(1; John Lenon; Street1 City1; 31
2; Paul McCartney; Street2 City2; 32
3; Ringo Starr; Street3 City3; 33
4; George Harrison; Address4; 34
)"};
int main()
{
// Define Variable and read complete source file
std::vector<Bio> bio{std::istream_iterator<Bio>(sourceFile), std::istream_iterator<Bio>()};
// Sort the guys by name
std::sort(bio.begin(), bio.end(), [](const Bio& b1, const Bio& b2){ return b1.name < b2.name;});
// Show output on screen
std::copy(bio.begin(),bio.end(),std::ostream_iterator<Bio>(std::cout, "\n"));
return 0;
}
我们有一个新的源格式并且 main 没有改变。只是提取器运算符被修改。这里我们使用不同的迭代器来获取源数据。