简体   繁体   English

向量的字符串,列表或其他内容? C ++

[英]Vector of Strings, List or something else? C++

I Currently have a list of data that has been auto-collaborated into a txt file. 我目前有一个已自动协作到txt文件中的数据列表。 Currently I am loading it into a buffer segmenting it based on new lines and adding each line to a vector string. 目前,我正在将其加载到缓冲区中,根据新行对其进行分段,并将每行添加到向量字符串中。 End result currently: I have a vector of strings (confirmed working with cout). 当前最终结果:我有一个向量字符串(已确认可与cout一起使用)。

My problem is that I am going to be needed to remove various parts of each and in some cases cut parts and move them around for formatting reasons, similar to databases. 我的问题是,需要删除每个部分的各个部分,并且在某些情况下,出于格式化的原因,它们会被切掉并四处移动,类似于数据库。

Eg 例如

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

I need to cut line 8 and append it back to the end of Carolyn Rodriquez. 我需要剪切第8行并将其附加到Carolyn Rodriquez的末尾。 I seem to be having difficulties trying to implement the string commands with vector class doing a test for size. 我似乎很难用向量类来实现字符串命令,以进行大小测试。

vector<string> myvect;
//...

if(strlen(myvect[2])<16)
    myvect.erase (i);

End result is a CSV file with first and last name per line, that is all. 最终结果是一个CSV文件,每行只有名字和姓氏。 How should I address this with inbuilt libraries? 我应该如何使用内置库解决这个问题? Am I looking into the right application or would lists or another method be much easier? 我在寻找合适的应用程序还是列表或其他方法会容易得多?


What I have thus far: 到目前为止,我有:

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;
}

As far as storage goes, I think stijn's idea is a good one to store the name as pairs in your vector, but what-ever is easyest for you! 就存储而言,我认为stijn的想法是将名称成对存储在向量中的一个好主意,但是对您来说最简单的方法是!

Here is some crude quickly written and un-tested example code to get you going: 这是一些快速编写且未经测试的示例代码,帮助您入门:

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));
}

...A rough example how you can make your list trying not to change your code too much :) If you want to move stuff around later, use vector.erase() and vector.insert() to delete and add enteies. ...一个粗略的示例,说明如何使列表尽量不更改代码:)如果以后要移动内容,请使用vector.erase()和vector.insert()删除并添加敌人。

Alternativley if you want to store your whole lines as std::strings then you can use the substr() funtction to pick out a part of that string. 如果您想将整行存储为std :: strings,则可以使用substr()函数来选择该字符串的一部分。

I would read data into a suitable struct and print back what I need 我会将数据读入合适的结构并打印出我需要的内容

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM