草庐IT

my-first-article

全部标签

c++ - 如何从 udp 端点获取 *my* ip

Boost.Asio的udp::endpoint有一个成员是远程地址。因为我在多个接口(interface)上监听(像这样):udp_socket(io_service,udp::endpoint(udp::v4(),port))在我的处理程序中,我不知道哪个网络接口(interface)收到了数据包。如果不遍历网络接口(interface)并在每个接口(interface)上寻找端点地址和我的IP之间的相似性,我能否获得我从中获取消息的接口(interface)的IP? 最佳答案 没有。Boost.Asio不提供识别数据报目标地址

c++ - 这里的 (void) first2++ 有什么意义?

这个问题在这里已经有了答案:Whydoesstd::transformandsimilarcastthe'for'loopincrementto(void)?(2个答案)关闭5年前。关于thisen.cppreference的页面有可能实现词典顺序比较的示例。这是基本的:templateboollexicographical_compare(InputIt1first1,InputIt1last1,InputIt2first2,InputIt2last2){for(;(first1!=last1)&&(first2!=last2);first1++,(void)first2++){if

c++ - std::error_code,my_error::check_block == my_error::validate && my_error::accept_block == my_error::validate

我正在使用std::error_code并定义和注册了一堆错误(使用枚举类)。我有一个非常通用的错误,现在称为my_error::validate,但我想在我的库中提供更具体的版本。通常人们会想要使用:if(ec==bc::error::validate)//...但是有时他们可能希望看到与该std::error_code关联的特定错误或打印错误消息。//ec.message()says"check_block()failedtodoXYZ"assert(ec==bc::error::check_block);我希望能够启用如下功能:if(ec==bc::error::validate

C++ 范围-v3 库 : 'take' -ing first 3 perfect numbers works and halts; 'take' -ing first 4 doesn't stop after 4

据我了解,range-v3库的View操作(目前需要C++17,但要成为C++20中STL的正式部分)提供了可链接的类STL算法,这些算法是延迟计算的。作为实验,我创建了以下代码来评估前4个完全数:#include#includeusingnamespacestd;intmain(intargc,char*argv[]){autoperfects=ranges::view::ints(1)|ranges::view::filter([](intx){intpsum=0;for(inty=1;y代码以可能无限范围的数字开始(ranges::view::ints(1)),但是因为View算

c++ - 搜索空字符串时 find vs find_first_of

在STL中,当我执行s.find("")时,它返回0而s.find_first_of("")返回-1(npos)。造成这种差异的原因是什么? 最佳答案 s.find(t)查找子字符串t在s中的第一次出现。如果t为空,则该事件出现在s的开头,并且s.find(t)将返回0。s.find_first_of(t)在t中查找第一次出现的一个字符。如果t为空字符串,则t中没有字符,所以找不到匹配项,find_first_of将返回npos.Liveonideone. 关于c++-搜索空字符串时fi

C++: "my text"是 std::string、*char 还是 c 字符串?

我刚刚做了看起来是acommonnewbiemistake的事情:首先我们阅读oneofmanytutorials是这样的:#includeintmain(){usingnamespacestd;ifstreaminf("file.txt");//(...)}其次,我们尝试在我们的代码中使用类似的东西,它是这样的:#includeintmain(){usingnamespacestd;std::stringfile="file.txt";//Orgetthenameofthefile//fromafunctionthatreturnsstd::string.ifstreaminf(fi

c++ - 为什么 "begin/end"与 "first/last"存在差异?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭5年前。Improvethisquestion为什么容器提供"begin"/"end"迭代器而算法需要"first"/"last"迭代器?例如:vector提供.begin()和.end()(cppreference.com,cplusplus.com)。sort需要参数first和last(cppreference.com,cplusplus.com)。编辑:发现一个更大的差异。不仅仅是算法使用“first/last”,它也是容器构

c++ - 为什么 std::find_if(first, last, p) 不通过引用获取谓词?

我正在查看std::find_ifoncppreference.com,的各种签名我注意到采用谓词函数的flavors似乎按值接受它:templateInputItfind_if(InputItfirst,InputItlast,UnaryPredicatep);如果我理解正确的话,具有捕获变量的lambda会为其数据的引用或拷贝分配存储空间,因此“按值传递”可能意味着为调用复制了捕获数据的拷贝。另一方面,对于函数指针等可直接寻址的东西,如果直接传递函数指针,性能应该会更好,而不是通过引用到指针(pointer-to-pointer)。首先,这是正确的吗?上面的UnaryPredica

c++ - 如何模拟不存在的 find_first_not_of 函数?

std::basic_string类模板有成员函数find_first_of和find_first_not_of。然而,header只包含一个通用的find_first_of。问题1:是缺席std::find_first_not_of(Iter1first1,Iter1last1,Iter2first2,Iter2last2)只是一个疏忽(例如copy_if)还是故意省略,因为该行为可以通过另一个标准函数实现?当然我可以自己写find_first_not_of,但是问题2:中是否有现成的解决方法??例如,缺少copy_if由remove_copy_if的存在补偿提前致谢

C++ 函数对象返回 `p->first` 和 `p->second`

有没有返回p->first和p->second的内置函数对象,让我可以愉快的写transform(m.begin(),m.end(),back_inserter(keys),get_first);transform(m.begin(),m.end(),back_inserter(vals),get_second);基于STL的解决方案是最好的,boost解决方案次之。是的,我知道boost::lambda,我不想开始使用它。 最佳答案 g++有非标准扩展和SGI称为select1st和select2nd。因此,STL中可能没有任何内