- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当输入字符串为空时,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
最佳答案
压缩会压缩相邻的分隔符,不会避免空标记。
如果您考虑以下几点,您就会明白为什么它始终有效:
#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' ''
因此,您可以通过修剪前端和末尾的分隔符并检查剩余输入是否为非空来修复它:
#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 更灵活,而且可能更高效:
#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/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!