BOOST :: Split即使使用Token_compress_on将一个空字符串推向向量
boost::split pushes an empty string to the vector even with token_compress_on
当输入字符串为空白时, boost::split
返回一个带有一个空字符串的向量。
是否可以让boost::split
返回空矢量?
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
压缩压缩相邻的定界符,它不能避免空白令牌。
如果考虑以下内容,则可以看到为什么这始终如一:
活在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' ''
因此,您可以通过从前和结束的修剪定界数来修复它,并检查其余的输入是非空的:
活在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 X3,在我看来,更灵活,可能更有效:
活在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++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 使用 std::transform 将向量向量 (a) 添加到另一个向量向量 (b)
- 如何按另一个向量的方向调整一个向量?
- 将字符向量复制到另一个向量
- 制作一对共享指针并推送一个向量
- 你能把一个向量<int64>投射到一个向量<uint8>吗
- 大家好,当一个类有一个向量作为它的数据成员时,为什么它的大小总是24?
- 为什么我们需要在优先级队列声明中添加一个向量作为参数?
- 如何将所有指针从一个向量移动到另一个向量?
- 将一个向量映射到不同的对,即使向量映射到每对时是不同的?
- 生成一个类Name_class并将两种数据类型存储在一个向量中
- 使用步骤c++构建一个向量
- 如何从另一个向量中的另一个第一元素减去向量中的第一元素
- 将一个向量对放在一个无序映射与一个映射中