c++ 中跳过重复字符字符串处理
我正在编写一个需要处理字符串(char *)的高性能函数。
这些字符串通常很长,但包含重复的字符,这些重复的字符在处理完字符后就不起作用。
我已经实现了一个 std::set 来存储处理后的字符,并在处理之前检查字符是否不在集合中。
您能想到更有效的方法吗?
谢谢
解决方案:
我选择了布尔数组。
bool b[256] = {0};
...
if(!b[*ci]){
b[*ci]=true;
...
}
感谢您的帮助!
I'm writing a high performance function that needs to process a string (char *).
These strings are often very long but contain duplicate characters which have no effect once the character has been processed.
I've implemented an std::set to store the processed characters and check the character is not in the set before processing.
Is there a more efficient method you can think of?
Thanks
SOLUTION:
I went for a bool array.
bool b[256] = {0};
...
if(!b[*ci]){
b[*ci]=true;
...
}
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是一个与字符数一样长的数组,并勾选数组中的字符。
just an array that is as long as the number of characters and tick off the char within the array.
您需要一个初始化为 0 的 256 位(32 字节)列表,然后在看到字符时设置这些位。创建该数据类型的最简单方法是将其分成 4 个 8 字节整数,然后您可以检查字符的范围以查看要检查/写入的 int。
You need a 256bit (32 byte) list that is initialised to 0, and then you set the bits as you see a character. The easiest way to make that data type would be to split it into 4 lots of 8 byte integers, and then you can check the range of the character to see which int to check/write to.