简体   繁体   中英

Vector of Strings, List or something else? C++

I Currently have a list of data that has been auto-collaborated into a txt file. 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).

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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