我有一个txt文件,其中包含姓名、id号、移动号和位置,用逗号分隔。示例Robby,7890,7788992356,123 westminister tom,8820,77882345,124Kingston road我的任务是检索
按姓名查找员工的所有信息。按ID查询所有员工信息,添加员工信息。更新员工信息。
到目前为止,我已经读取了该文件,并将信息存储在一个向量中。代码如下所示。对于任务1)按姓名查找员工的所有信息。我将在向量中迭代并打印包含名称的信息。我将能够在文本文件中执行类似的操作,我将查找id并打印相关信息。但我对第三点和第四点一无所知。
我在下面发布了我的代码
void filter_text( vector<string> *words, string name)
{
vector<string>::iterator startIt = words->begin();
vector<string>::iterator endIt = words->end();
if( !name.size() )
std::cout << " no word to found for empty string ";
while( startIt != endIt)
{
string::size_type pos = 0;
while( (pos = (*startIt).find_first_of(name, pos) ) != string::npos)
std:cout <<" the name is " << *startIt<< end;
startIt++;
}
}
int main()
{
// to read a text file
std::string file_name;
std::cout << " please enter the file name to parse" ;
std::cin >> file_name;
//open text file for input
ifstream infile(file_name.c_str(), ios::in) ;
if(! infile)
{
std::cerr <<" failed to open file\n";
exit(-1);
}
vector<string> *lines_of_text = new vector<string>;
string textline;
while(getline(infile, textline, '\n'))
{
std::cout <<" line text:" << textline <<std::endl;
lines_of_text->push_back(textline);
}
filter_text( lines_of_text, "tony");
return 0;
}发布于 2011-10-13 08:25:30
#include <string>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <fstream>
struct bird {
std::string name;
int weight;
int height;
};
bird& find_bird_by_name(std::vector<bird>& birds, const std::string& name) {
for(unsigned int i=0; i<birds.size(); ++i) {
if (birds[i].name == name)
return birds[i];
}
throw std::runtime_error("BIRD NOT FOUND");
}
bird& find_bird_by_weight(std::vector<bird>& birds, int weight) {
for(unsigned int i=0; i<birds.size(); ++i) {
if (birds[i].weight< weight)
return birds[i];
}
throw std::runtime_error("BIRD NOT FOUND");
}
int main() {
std::ifstream infile("birds.txt");
char comma;
bird newbird;
std::vector<bird> birds;
//load in all the birds
while (infile >> newbird.name >> comma >> newbird.weight >> comma >> newbird.height)
birds.push_back(newbird);
//find bird by name
bird& namebird = find_bird_by_name(birds, "Crow");
std::cout << "found " << namebird.name << '\n';
//find bird by weight
bird& weightbird = find_bird_by_weight(birds, 10);
std::cout << "found " << weightbird.name << '\n';
//add a bird
std::cout << "Bird name: ";
std::cin >> newbird.name;
std::cout << "Bird weight: ";
std::cin >> newbird.weight;
std::cout << "Bird height: ";
std::cin >> newbird.height;
birds.push_back(newbird);
//update a bird
bird& editbird = find_bird_by_name(birds, "Raven");
editbird.weight = 1000000;
return 0;
}显然不是员工,因为那会让你的家庭作业太容易。
发布于 2011-10-13 08:21:32
首先将CSV行拆分为多个单独的字段,然后使用此数据填充结构
例如:
struct Employee
{
std::string name;
std::string id_number;
std::string mobilenumber;
std::string location;
};
std::vector<Employee> employees; // Note you dont need a pointer看看字符串方法find_first_of、substr和friends。
发布于 2011-10-13 08:24:20
所以,首先,我认为不应该将信息存储在字符串向量中。这类任务完全需要使用
struct employee {
int id;
std::string name;
std::string address;
//... more info
};并将雇员的实例存储在
std::vector<employee>您看,使用您存储行的策略,搜索" westminster“将使我获得Robbie,因为他的文本行确实包含此子字符串,但他的名字根本不是westminster。将数据存储在employee结构的向量中可以消除这个问题,而且会使整个事情变得更加结构化。
当然,您需要实际解析该文件才能将信息转换为向量。我建议使用这样的策略:
while(getline(infile, textline, '\n')) {
std::stringstream l(textline);
getline(l,oneEmp.name, ','); //extract his name using getline
l >> oneEmp.id; //extract his id
//extract other fields from the stringstream as neccessary
employees.push_back(oneEmp);
}至于添加信息:当用户输入数据时,只需将其存储在您的employees矢量中;当您需要更新文件时,您可以简单地用一个新的数据文件覆盖原始数据文件,方法是打开它以便写入并将数据转储到那里(这显然是一个相当浪费的策略,但它对于学校作业(我假设它是学校作业)是很好的)。
https://stackoverflow.com/questions/7748004
复制相似问题