[英]Vector of Strings, List or something else? C++
我目前有一个已自动协作到txt文件中的数据列表。 目前,我正在将其加载到缓冲区中,根据新行对其进行分段,并将每行添加到向量字符串中。 当前最终结果:我有一个向量字符串(已确认可与cout一起使用)。
我的问题是,需要删除每个部分的各个部分,并且在某些情况下,出于格式化的原因,它们会被切掉并四处移动,类似于数据库。
例如
1. Bernice Armstrong asd-ssdd 123123
2. Johnathan Potter asd-ssdd 123123
3. Kay Dixon asd-ssdd 123123
4. Melba Barton asd-ssdd 123123
5. Alison Malone asd-ssdd 123123
6. Mercedes Hale asd-ssdd 123123
7. Carolyn Rodriquez
asd-ssdd 123123
8. Norma Banks asd-ssdd 123123
9. Homer Burke asd-ssdd 123123
10. Mary Kelly asd-ssdd 123123
我需要剪切第8行并将其附加到Carolyn Rodriquez的末尾。 我似乎很难用向量类来实现字符串命令,以进行大小测试。
vector<string> myvect;
//...
if(strlen(myvect[2])<16)
myvect.erase (i);
最终结果是一个CSV文件,每行只有名字和姓氏。 我应该如何使用内置库解决这个问题? 我在寻找合适的应用程序还是列表或其他方法会容易得多?
到目前为止,我有:
int PostProcess(std::ofstream &file, char* pbuffer, size_t len)
{
char * buffer = pbuffer;
char tmp[160];
vector<string> data;
string line;
unsigned int initial = 0;
int count = 0;
//dynamically assign array of memory
for (unsigned int pos = 0; pos< len;pos++)
{
tmp[count] = *buffer;
if ((*buffer) == '\n')
{
tmp[count+1] = 0;
line = tmp;
data.push_back(line);
initial = pos;
}
if (count >= 150)
break;
buffer++;
count = pos - initial;
}
//debugger - shows all data in vector
for (std::vector<string>::const_iterator i = data.begin(); i != data.end(); ++i)
std::cout << *i << ' ';
//end debug code
buffer = pbuffer;
file << buffer;
return 0;
}
就存储而言,我认为stijn的想法是将名称成对存储在向量中的一个好主意,但是对您来说最简单的方法是!
这是一些快速编写且未经测试的示例代码,帮助您入门:
char *start = pBuffer;
char *end = pBuffer;
char *max = (pBuffer + len);
vector<pair<string, string>> name_vect;
for (unsigned int pos = 0; pos < len; pos++)
{
string name_first;
string name second;
// Get first name
while (*end != ' ' && end <= max) end++;
name_first.assign(start, end - start);
start = ++end; // move pointers on
// Get second name
while (*end != ' ' && end <= max) end++;
name_second.assign(start, end - start);
start = ++end; // move pointers on
// Move to the end of the line
while (*end != '\n' && end <= max) end++;
start = ++end; // move pointers on
// Now we have a named pair add them to the vector
name_vect.pushback(std::make_pair (name_first, name_second));
}
...一个粗略的示例,说明如何使列表尽量不更改代码:)如果以后要移动内容,请使用vector.erase()和vector.insert()删除并添加敌人。
如果您想将整行存储为std :: strings,则可以使用substr()函数来选择该字符串的一部分。
我会将数据读入合适的结构并打印出我需要的内容
#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
struct Data
{
size_t num;
std::string name;
std::string surname;
std::string code;
size_t id;
};
std::istream& operator>>(std::istream& is, Data& d)
{
is >> d.num;
is.ignore(2); // skip '. '
is >> d.name; // take into consideration that
is >> d.surname; // a name or surname like 'de palma' will
is >> d.code; //not work
is >> d.id;
return is;
}
std::ostream& operator<<(std::ostream& os, const Data& d)
{
// Do everything you need here
// in term of formatting
os << d.name << "," << d.surname;
return os;
}
int main(int argc, char *argv[])
{
if (argc == 1) { std::cerr << "please give me a file to process" << std::endl; return 0; }
std::ifstream ifStream(argv[1]); // consider to have a file with data in it
std::istream_iterator<Data> begin(ifStream);
std::istream_iterator<Data> end;
std::vector<Data> data (begin, end);
std::copy(data.begin(),
data.end(),
std::ostream_iterator<Data>(std::cout, "\n"));
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.