字符串向量、列表或其他什么?C++
Vector of Strings, List or something else? C++
I当前有一个已自动协作到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.esers()和vector.insert()来删除和添加词条。
或者,如果您想将整行存储为std::string,那么您可以使用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;
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用