gpt4 book ai didi

c++ - boost::split 即使使用 token_compress_on 也会将空字符串推送到 vector

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:51 64 4
gpt4 key购买 nike

当输入字符串为空时,boost::split返回一个包含一个空字符串的 vector 。

是否可以让 boost::split 返回一个空 vector ?

MCVE:

#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>

int main() {
std::vector<std::string> result;
boost::split(result, "", boost::is_any_of(","), boost::algorithm::token_compress_on);
std::cout << result.size();
}

输出:

1

期望的输出:

0

最佳答案

压缩会压缩相邻的分隔符,不会避免空标记。

如果您考虑以下几点,您就会明白为什么它始终有效:

Live On Coliru

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>

int main() {
for (std::string const& test : {
"", "token",
",", "token,", ",token",
",,", ",token,", ",,token", "token,,"
})
{
std::vector<std::string> result;
boost::split(result, test, boost::is_any_of(","), boost::algorithm::token_compress_on);
std::cout << "\n=== TEST: " << std::left << std::setw(8) << test << " === ";
for (auto& tok : result)
std::cout << std::quoted(tok, '\'') << " ";
}
}

打印

=== TEST:          === '' 
=== TEST: token === 'token'
=== TEST: , === '' ''
=== TEST: token, === 'token' ''
=== TEST: ,token === '' 'token'
=== TEST: ,, === '' ''
=== TEST: ,token, === '' 'token' ''
=== TEST: ,,token === '' 'token'
=== TEST: token,, === 'token' ''

因此,您可以通过修剪前端和末尾的分隔符并检查剩余输入是否为非空来修复它:

Live On Coliru

#include <boost/algorithm/string.hpp>
#include <boost/utility/string_view.hpp>
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>

int main() {
auto const delim = boost::is_any_of(",");

for (std::string test : {
"", "token",
",", "token,", ",token",
",,", ",token,", ",,token", "token,,"
})
{
std::cout << "\n=== TEST: " << std::left << std::setw(8) << test << " === ";

std::vector<std::string> result;

boost::trim_if(test, delim);
if (!test.empty())
boost::split(result, test, delim, boost::algorithm::token_compress_on);

for (auto& tok : result)
std::cout << std::quoted(tok, '\'') << " ";
}
}

打印:

=== TEST:          === 
=== TEST: token === 'token'
=== TEST: , ===
=== TEST: token, === 'token'
=== TEST: ,token === 'token'
=== TEST: ,, ===
=== TEST: ,token, === 'token'
=== TEST: ,,token === 'token'
=== TEST: token,, === 'token'

奖励:提振 spirit

在我看来,使用 Spirit X3 更灵活,而且可能更高效:

Live On Coliru

#include <boost/spirit/home/x3.hpp>
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>

int main() {
static auto const delim = boost::spirit::x3::char_(",");

for (std::string test : {
"", "token",
",", "token,", ",token",
",,", ",token,", ",,token", "token,,"
})
{
std::cout << "\n=== TEST: " << std::left << std::setw(8) << test << " === ";

std::vector<std::string> result;
parse(test.begin(), test.end(), -(+~delim) % delim, result);

for (auto& tok : result)
std::cout << std::quoted(tok, '\'') << " ";
}
}

关于c++ - boost::split 即使使用 token_compress_on 也会将空字符串推送到 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46540255/

64 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com