c++ - 将数据字符串标记为结构 vector ?

标签 c++ string tokenize winsock2

因此,我有以下数据字符串,该数据是通过 TCP winsock 连接接收的,并且希望将其高级标记化为结构 vector ,其中每个结构代表一条记录。

std::string buf = "44:william:adama:commander:stuff\n33:luara:roslin:president:data\n"

struct table_t
{
    std::string key;
    std::string first;
    std::string last;
    std::string rank;
    std::additional;
};

字符串中的每条记录均由回车符分隔。我尝试拆分记录,但尚未拆分字段:

    void tokenize(std::string& str, std::vector< string >records)
{
    // Skip delimiters at beginning.
    std::string::size_type lastPos = str.find_first_not_of("\n", 0);
    // Find first "non-delimiter".
    std::string::size_type pos     = str.find_first_of("\n", lastPos);
    while (std::string::npos != pos || std::string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        records.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of("\n", pos);
        // Find next "non-delimiter"
        pos = str.find_first_of("\n", lastPos);
    }
}

似乎完全没有必要再次重复所有这些代码,以通过冒号(内部字段分隔符)将每个记录进一步标记到结构中,并将每个结构插入 vector 中。我确信有更好的方法可以做到这一点,或者设计本身就是错误的。

感谢您的帮助。

最佳答案

我的解决方案:

struct colon_separated_only: std::ctype<char> 
{
    colon_separated_only(): std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        typedef std::ctype<char> cctype;
        static const cctype::mask *const_rc= cctype::classic_table();

        static cctype::mask rc[cctype::table_size];
        std::memcpy(rc, const_rc, cctype::table_size * sizeof(cctype::mask));

        rc[':'] = std::ctype_base::space; 
        return &rc[0];
    }
};

struct table_t
{
    std::string key;
    std::string first;
    std::string last;
    std::string rank;
    std::string additional;
};

int main() {
        std::string buf = "44:william:adama:commander:stuff\n33:luara:roslin:president:data\n";
        stringstream s(buf);
        s.imbue(std::locale(std::locale(), new colon_separated_only()));
        table_t t;
        std::vector<table_t> data;
        while ( s >> t.key >> t.first >> t.last >> t.rank >> t.additional )
        {
           data.push_back(t);
        }
        for(size_t i = 0 ; i < data.size() ; ++i )
        {
           cout << data[i].key <<" ";
           cout << data[i].first <<" "<<data[i].last <<" ";
           cout << data[i].rank <<" "<< data[i].additional << endl;
        }
        return 0;
}

输出:

44 william adama commander stuff
33 luara roslin president data

在线演示:http://ideone.com/JwZuk


我在这里使用的技术在我对不同问题的另一个解决方案中进行了描述:

Elegant ways to count the frequency of words in a file

关于c++ - 将数据字符串标记为结构 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5462022/

相关文章:

java - 在 O(1) 中反转 Java 中的字符串?

CS50 集成开发环境 : printf returns extra characters

string - 如何拆分这种 : "howdoIsplitthis?" 的连接字符串

python - 使用 Look Behind 或 Look Ahead 函数查找匹配项时的正则表达式模式

python - 当且仅当它在输入字符串中找到枚举时,如何创建删除等于子字符串的元素的正则表达式模式?

c++ - 内置类型的运算符函数

java - 从现有模型创建 OpenCV Haar 分类器

c++ - 在 netbeans 中包含 hdf5 的库

c# - SQLite 代码分析 C#

c++ - 为 i686-elf 交叉编译和链接 libstdc++(在 Ubuntu 16.04 上使用 g++)