草庐IT

boolean-search

全部标签

c++ - 在计算中使用 boolean 值以避免分支

这是我想出的一些微优化的好奇心:structTimer{boolrunning{false};intticks{0};voidstep_versionOne(intmStepSize){if(running)ticks+=mStepSize;}voidstep_versionTwo(intmStepSize){ticks+=mStepSize*static_cast(running);}};这两种方法似乎实际上做同样的事情。第二个版本是否避免了分支(因此比第一个版本更快),或者是否有任何编译器能够使用-O3进行这种优化? 最佳答案

c++ - 为什么不能将 C++ 设置迭代器强制转换为 boolean 值?

对于一个STL集,您似乎应该能够说:if(s.find(x)){//Something}相对于if(s.find(x)!=s.end()){//Something}此外,如果可以将set-iterators强制转换为bool(如果内部指针不为null,则为true),您就可以做到。为什么STL集合迭代器没有这个简单的功能?这是故意遗漏的吗?澄清:或者,set可以有一个直接返回bool的set::contains(x)方法,但这似乎也没有实现。我知道它只有几个字符,但在s是某个函数的返回值的情况下,这可能会令人沮丧,因为需要创建一个临时变量,即(假设m的类型为map>)constset&

c++ - 使用 boost::regex_search 忽略大小写

如何在C++中使用boost::regex_search忽略大小写标志或常量?请发布一个简单的示例。谢谢! 最佳答案 你需要这样的东西boost::regexregex("yourexpressionhere",boost::regex::icase);boost::smatchwhat;stringmystring;boolsearch_result=boost::regex_search(mystring.begin(),mystring.end(),what,regex); 关于c

c++ - libc++ std::search_n 中的崩溃是一个错误吗?

我已经尽可能地缩小了范围,这似乎是一个错误......#include#includeintmain(intargc,char*argv[]){//Crashesstd::vectorbs{1,0,0};std::search_n(bs.begin(),bs.end(),3,1);//Doesnotcrashstd::vectorbs{1,0};std::search_n(bs.begin(),bs.end(),2,1);return0;}我明白了Segmentationfault:11我希望我没有错误地使用std::search_n:)目前使用LLDB似乎不可能逐步完成STL实现。版

c++ - 如何循环 std::regex_search 的结果?

调用std::regex_search后,出于某种原因,我只能从std::smatch获取第一个字符串结果:Expression.assign("rel=\"nofollow\">(.*?)");if(std::regex_search(Tables,Match,Expression)){for(std::size_ti=1;i所以我尝试用另一种方式-使用迭代器:conststd::sregex_token_iteratorEnd;Expression.assign("rel=\"nofollow\">(.*?)");for(std::sregex_token_iteratori(Ta

c++ - 在 `std::search` 上使用 `string::find`

我有一个关于使用std::search与string::find处理字符串的问题。我知道使用特定于类的成员函数算法通常比标准库算法更好,因为它可以基于类进行优化,但我想知道为了一致性,使用std是否合理::search使用迭代器而不是string::find使用索引。做那样的事情对我来说是一种罪过还是我应该坚持使用string::find?两者在性能或风格方面是否有任何巨大优势? 最佳答案 现在(2017年4月27日),至少GCCslibstdc++(默认情况下也被clang使用)实现了std::string::find线性搜索,因

c++ - 重载 boolean/字符串歧义

为什么C++将我传入的字符串文字转换为boolean值而不是字符串?#includeusingnamespacestd;classA{public:A(stringv){cout输出:1是不是因为编译器不够聪明,无法从char*跳转到string而只是假设bool是最接近指针的东西?我唯一的选择是制作一个基本上与字符串构造函数完全相同的显式char*构造函数吗? 最佳答案 如果你有C++11,你可以使用委托(delegate)构造函数:A(charconst*s):A(std::string(s)){}选择boolean转换构造函数

Windows Batch : Search all files in file, 如果行包含 "apple"或 "tomato"回显它

我正在尝试编写一个简单的批处理,它将遍历文件中的每一行,如果该行包含“apples”或“tomato”,则输出该行。我有这段代码可以找到一个字符串并输出它,但我无法在同一批处理中得到第二个。我还希望它在找到它们时回显这些行。@echoOFFfor/f"delims="%%Jin('findstr/ilc:"apple""test.txt"')do(echo%%J)它需要找到包含“apples”或“tomato”的行我可以使用我需要的两行轻松运行上面的代码,但我需要将这些行相互输出。例如我需要:appletomatotomatoappletomatoappleapple不是:applea

sql - OLE DB 提供程序 "Search.CollatorDSO"返回 "Command was not prepared"

是否有人能够使用OLEDBProviderforSearchinSQLServer连接到WindowsSearchService?下面是我在ManagementStudio中配置链接服务器时不断遇到的错误消息。OLEDBprovider"Search.CollatorDSO"forlinkedserver"TESTSERVER"returnedmessage"Commandwasnotprepared.".Msg7399,Level16,State1,Line2TheOLEDBprovider"Search.CollatorDSO"forlinkedserver"TESTSERVER"

windows - 批处理文件 : What's the best way to declare and use a boolean variable?

在批处理文件中声明和使用bool变量的最佳方式是什么?这就是我现在正在做的:set"condition=true"::Somecodethatmaychangetheconditionif%condition%==true(::Somework)是否有更好、更“正式”的方式来做到这一点?(例如,在Bash中,您可以只执行if$condition,因为true和false是它们自己的命令。) 最佳答案 set"condition="和set"condition=y"其中y可以是任何字符串或数字。这允许ifdefined和ifnotde