有很多方法可以处理读取包含未知数量的空格分隔部分和尾随数字的名称。您可以使用cstdio 轻松做到这一点,并使用getline() 读取每一行,然后使用str.c_str() 的格式字符串" %[^0-9] %zu" 调用sscanf(),然后在分配给a 之前从temporary_name 修剪尾随空格字符串。
停留在当前时代的 C++ 中,您可以读取带有getline 的行,然后使用.find_first_of() 成员函数来定位字符串中的第一个数字。例如,您可以保留一个数字列表,例如const char *digits = "0123456789";,然后用line.find_first_of(digits); 定位第一个数字。知道第一个数字在哪里,然后您可以使用.substr() 成员函数复制name,然后从末尾去除尾随空格。
更大的考虑是如何存储所有读取的值。如果您创建一个简单的struct,其中包含成员std:string name; 和size_t pop;,那么您可以创建一个结构体std::vector,并使用.push_back() 成员函数添加从文件中读取的每个结构体数据,以添加一个将新结构添加到结构的向量中。
结构的简单实现可以是:
struct population
{
std::string name;
size_t pop;
/* constructors */
population() { name = ""; pop = 0; }
population(const std::string& n, const size_t p) : name(n), pop(p) {}
};
为了简化从文件中读取,您可以创建>> 的重载,它将从打开的文件流中读取一行数据,并为您将name 和pop 分开。 << 的第二个重载将允许您以您选择的合理格式输出结构。添加您将拥有的重载:
/* struct to hold name population,
* and overloads of operators >> and << to facilitate splitting name/hours.
*/
struct population
{
std::string name;
size_t pop;
/* constructors */
population() { name = ""; pop = 0; }
population(const std::string& n, const size_t p) : name(n), pop(p) {}
/* overloads of >> (separates name/pop) and << (outputs name/pop) */
friend std::istream& operator >> (std::istream& is, population& p) {
const char *digits = "0123456789";
std::string line {};
if (getline (is, line)) { /* read line */
size_t popbegin = line.find_first_of(digits); /* find 1st [0-9] */
if (popbegin != std::string::npos) { /* valdiate found */
std::string tmp = line.substr(0, popbegin); /* get name */
while (isspace(tmp.back())) /* remove trailing */
tmp.pop_back(); /* .. spaces */
p.name = tmp; /* assign to name */
p.pop = stoul(line.substr(popbegin)); /* assign to pop */
}
}
return is;
}
friend std::ostream& operator << (std::ostream& os, const population& p) {
os << std::left << std::setw(32) << p.name << " " << p.pop << '\n';
return os;
}
};
那么你在main() 中需要做的就是验证你有一个作为参数传递的文件名,打开文件并验证它是否可以读取(比如std::ifstream f),然后你的读取和值分离减少到一个简单的循环:
population p {}; /* instance of population struct to facilitate read from file */
std::vector<population> records {}; /* vector of population */
while (f >> p) { /* read population data from file */
records.push_back(p); /* add to population vector */
}
现在您将所有位置和人口都存储在 struct records 的向量中。总而言之,您可以这样做:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
/* struct to hold name population,
* and overloads of operators >> and << to facilitate splitting name/hours.
*/
struct population
{
std::string name;
size_t pop;
/* constructors */
population() { name = ""; pop = 0; }
population(const std::string& n, const size_t p) : name(n), pop(p) {}
/* overloads of >> (separates name/pop) and << (outputs name/pop) */
friend std::istream& operator >> (std::istream& is, population& p) {
const char *digits = "0123456789";
std::string line {};
if (getline (is, line)) { /* read line */
size_t popbegin = line.find_first_of(digits); /* find 1st [0-9] */
if (popbegin != std::string::npos) { /* valdiate found */
std::string tmp = line.substr(0, popbegin); /* get name */
while (isspace(tmp.back())) /* remove trailing */
tmp.pop_back(); /* .. spaces */
p.name = tmp; /* assign to name */
p.pop = stoul(line.substr(popbegin)); /* assign to pop */
}
}
return is;
}
friend std::ostream& operator << (std::ostream& os, const population& p) {
os << std::left << std::setw(32) << p.name << " " << p.pop << '\n';
return os;
}
};
int main (int argc, char **argv) {
if (argc < 2) { /* validate 1 argument given for filename */
std::cerr << "error: filename required as 1st argument.\n";
return 1;
}
std::ifstream f (argv[1]); /* open filename provided as 1st argument */
if (!f.is_open()) { /* validate file is open for reading */
std::cerr << "file open failed: " << argv[1] << '\n';
return 1;
}
population p {}; /* instance of population struct to facilitate read from file */
std::vector<population> records {}; /* vector of population */
while (f >> p) { /* read population data from file */
records.push_back(p); /* add to population vector */
}
for (const auto& loc : records) /* output results */
std::cout << std::left << std::setw(32) << loc.name << loc.pop << '\n';
}
使用/输出示例
在文件dat/population.txt 中使用您的数据,使用和结果将是:
$ ./bin/poprecords dat/population.txt
Jackson 49292
Levy 40156
Indian River 138894
Liberty 8314
Holmes 19873
Madison 19115
由于您将数据存储在结构向量中,因此您可以按照您喜欢的任何方式对向量进行排序以分析数据。
这只是解决问题的众多方法之一。如果您还有其他问题,请仔细查看并告诉我。