簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM