c++字符串二维数组解析|std::string::find_first_of 重新学习

11 篇文章 0 订阅
9 篇文章 0 订阅

参考: http://www.cplusplus.com/reference/string/string/find_first_of/

1.std::string::find_first_of

string (1)
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;

c-string (2)
size_t find_first_of (const char* s, size_t pos = 0) const;

buffer (3)
size_t find_first_of (const char* s, size_t pos, size_t n) const;

character (4)
size_t find_first_of (char c, size_t pos = 0) const noexcept;


Find character in string Searches the string for the first character that matches any of the characters specified in its arguments.
渣翻: (返回 字符串中查找第一个跟传入的参数pattern[中任意一个字符]匹配的位置size_t)

When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences before pos.
渣翻:(当参数中包含 pos参数, 函数只会从pos后的位置开始查找pattern中的字符位置,会忽略掉pos之前的字符.)

Notice that it is enough for one single character of the sequence to match (not all of them). See string::find for a function that matches entire sequences
渣翻:(这函数只会匹配单个字符,而不会匹配到整个字符串,想要匹配整段字符串的看 string::find ,fuck 😃)


2.std::string::find

string (1)
size_t find (const string& str, size_t pos = 0) const noexcept;

c-string (2)
size_t find (const char* s, size_t pos = 0) const;

buffer (3)
size_t find (const char* s, size_t pos, size_type n) const;

character (4)
size_t find (char c, size_t pos = 0) const noexcept;


Searches the string for the first occurrence of the sequence specified by its arguments.
渣翻:(查询第一个匹配参数中给出的字符串的位置,返回参数中字符串在源字符串中开始的位置,0开始数)
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.
渣翻:(当pos参数被指定,只会查询pos后面的字符串,忽略pos前面的字符串)
Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that just one of these characters match, but the entire sequence must match.
渣翻:(跟find_first_of不同, 一定会匹配到参数中整个字符串)


3.字符串数组读取 [[,],[,],[,],[,]]

struct stPoint {
    double lon;
    double lat;
};
std::vector<stPoint> praseStrArray(std::string strArray, std::string splitChar = ",", std::string head = "[", std::string tail = "]")
{
    std::vector<stPoint> ret;
    if (strArray.empty())
    {
        return ret;
    }
    std::string mid = tail + splitChar + head;  //分割
    size_t start = head.size()*2, index = strArray.find(mid);
    std::vector<std::string> vecTmp;
    while (index != strArray.npos)
    {
        if (start != index)
        {
            vecTmp.push_back(strArray.substr(start,index - start));
            start = index+ mid.size();
            index = strArray.find(mid, start);
        }
    }
    if (!strArray.substr(start).empty())
    {
        size_t finallength = strArray.size() - start - tail.size() * 2;
        vecTmp.push_back(strArray.substr(start, finallength));
    }
    //---
    for ( auto str: vecTmp)
    {
        size_t pos = str.find_first_of(splitChar);
        stPoint tmp;
        tmp.lon = atof(str.substr(0,pos).c_str());
        tmp.lat = atof(str.substr(pos + 1).c_str());
        ret.push_back(tmp);
    }
    return ret;
}


void test_strArray()
{
    std::string str = "[[13.0,55],[22.0,1.25],[0.48,545],[456.2,48.55],[74.2,16.5]]";
    std::vector<stPoint> rettt;
    rettt = praseStrArray(str);
}

4.split

//c++17
std::vector<std::string_view> splitSV(std::string_view strv, 
std::string_view delims = " ")
{
    std::vector<std::string_view> output;
    size_t first = 0;

    while (first < strv.size())
    {
        const auto second = strv.find_first_of(delims, first);

        if (first != second)
            output.emplace_back(strv.substr(first, second - first));

        if (second == std::string_view::npos)
            break;

        first = second + 1;
    }

    return output;
}

//c++11
std::vector<std::string> split(const std::string& str, const std::string& pattern)
{
    std::vector<std::string> ret;
    if (pattern.empty())
    {
        return ret;
    }
    size_t start = 0, index = str.find_first_of(pattern,0);
    while (index != str.npos)
    {
        if (start != index)
        {
            ret.push_back(str.substr(start,index-start));
        }
        start = index + 1;
        index = str.find_first_of(pattern, start);
    }
    if (!str.substr(start).empty())
    {
        ret.push_back(str.substr(start));
    }
    return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路途遥远gg

帮到你了就好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值