如何有效地从 std::string 中删除双引号(如果存在)
这个问题有重复的风险,例如 remove doublequotes from a string in c++< /a> 但我看到的答案都没有解决我的问题
我有一个字符串列表,其中一些是双引号的,有些不是,引号总是在开头和结尾
std::vector<std::string> words = boost::assign::list_of("words")( "\"some\"")( "of which")( "\"might\"")("be quoted");
我正在寻找删除引号的最有效方法。这是我的尝试
for(std::vector<std::string>::iterator pos = words.begin(); pos != words.end(); ++pos)
{
boost::algorithm::replace_first(*pos, "\"", "");
boost::algorithm::replace_last(*pos, "\"", "");
cout << *pos << endl;
}
我可以做得更好吗?我可能有数十万个字符串需要处理。它们可能来自文件或数据库。示例中的 std::vector 仅用于说明目的。
This question risks being a duplicate e.g. remove double quotes from a string in c++
but none of the asnwers that I saw addresses my question
I have a list of strings, some of which are double quoted and some aren't, Quotes are always at beginning and end
std::vector<std::string> words = boost::assign::list_of("words")( "\"some\"")( "of which")( "\"might\"")("be quoted");
I am looking for the most efficient way to remove the quotes. Here is my attempt
for(std::vector<std::string>::iterator pos = words.begin(); pos != words.end(); ++pos)
{
boost::algorithm::replace_first(*pos, "\"", "");
boost::algorithm::replace_last(*pos, "\"", "");
cout << *pos << endl;
}
Can I do better than this? I have potentially hundreds of thousands of string to process.They may come from a file or from a database. The std::vector in the example is just for illustration purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您知道引号将始终出现在第一个和最后一个位置,您可以简单地执行
复杂性仍然与字符串的大小成线性关系。您无法在 O(1) 时间内从
std::string
的开头插入或删除。如果可以接受用空格替换该字符,那么就这样做。If you know the quotes will always appear in the first and last positions, you can do simply
The complexity is still linear in the size of the string. You cannot insert or remove from the beginning of a
std::string
in O(1) time. If it is acceptable to replace the character with a space, then do that.进行检查可能会很快:
它可能不是有史以来最漂亮的东西,但它是 O(n) 且有一个小常数。
It would probably be fast to do a check:
It might not be the prettiest thing ever, but it's O(n) with a small constant.
现代 C++ 最有效的方法是:
基本原理:
erase()
函数修改字符串而不是重新分配它。The most efficient way for modern C++ is:
Rationale:
erase()
function modifies the string instead of reallocating it.front()
on empty strings triggers undefined behavior.erase
calls and optimize the code further (removing the first and last char together is a standard problem).这就是我处理这种情况的方法:
std::vector
时检查并取消引用字符串。如果您只是接收一个std::vector
,那么您无能为力,因为删除第一个引号将需要复制字符串的其余部分。This is how I would approach the situation:
std::vector<std::string>
in the first place. If you are simply receiving astd::vector<std::string>
there isn't too much you can do as removing the first quote will require copying the rest of the string.