草庐IT

c# - c++ - 使用 std 命名空间和依赖项

在尝试熟悉C++及其概念时,我遇到了usingnamespacestd和#include我的简单代码如下#include"stdafx.h"#include"ConsoleApplication5.h"#includeintmain(){std::cout使用使用intellisense的VisualStudio2015Community显示cout使用以下内容std::ostreamstd::cout作为一名C#程序员,这让我有些困惑。这是:std::ostream作为返回类型而std::cout是传递的方法/参数还是std::ostream依赖于cout更新(在Archimared

c++ - 如何获得 vector 的排序索引?

我有一个vector。它没有排序。现在我想得到它的索引,它将对vector进行排序。例如vectorv{1,3,2},排序索引为{0,2,1}因为v[0].如果两个相等,哪个先走并不重要。 最佳答案 您正在寻找的称为标记排序(或索引排序)。这是在C++11中使用lambda的最小示例:#include#include#include#includetemplatestd::vectortag_sort(conststd::vector&v){std::vectorresult(v.size());std::iota(std::beg

c++ - 线程: Termination of infinite loop thread in c++

我试图编写一个线程,该线程将在我的主程序的后台运行并监视某事。在某个时候,主程序应该向线程发出信号以使其安全退出。这是一个最小示例,该示例以固定的时间间隔将本地时间写入命令行。#include#include#include#include#includeintfunc(bool&on){while(on){autot=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());std::coutfi=std::async(std::launch::async,func,on);std::this_thr

c++ - 使用 std::function 包装函数对象

谁能帮我理解为什么下面的代码会导致错误?classA{public:float&operator()(){return_f;}private:float_f=1;}a;auto&foo(){std::functionfunc=a;returnfunc();}intmain(){std::cout错误:error:non-constlvaluereferencetotype'float'cannotbindtoatemporaryoftype'float'returnfunc();^~~~~~1errorgenerated.在这里,在operator()中,我返回了对_f的引用,因此,我

c++ - libstdc++ 的 std::vector<bool>::data 有什么作用?

根据标准,std::vector没有成员函数data().但是,以下代码片段可以使用带有libstdc++的最新GCC正常编译:#includeintmain(){std::vectorv;v.data();}如果我们尝试使用结果,结果返回类型是void.这是一些gcc扩展还是一个错误?如果前者为真,它有什么作用? 最佳答案 我的/usr/include/c++/4.8/bits/stl_bvector.h有://_GLIBCXX_RESOLVE_LIB_DEFECTS//DR464.Suggestionfornewmemberfu

c++ - std::vector 保留后 operator[] 上 msvc 和 gcc 之间的行为差​​异,哪个是对的?

Thissnippetofcode使用msvc(越界错误)惨遭失败,但似乎在gcc和clang上都能正常工作。什么是正确的行为?#include#includeintmain(){std::vectorv;v.reserve(10);for(inti=0;i 最佳答案 行为未定义。reserve只保留内存,不影响容器的大小。也许您想使用resize?std::vectorv;v.resize(10);for(inti=0;i虽然在这种情况下你可以写std::vectorv(10);for(inti=0;i或者,您可以将reserve

c++ - 如何在编译时检测 C++ 中的 std::reference_wrapper

假设我们有一些可变参数模板,需要以不同方式处理std::reference_wrapper参数。我们怎样才能做到这一点? 最佳答案 你可以做一个特征来判断一个类型是否是reference_wrappertemplatestructis_reference_wrapper:false_type{};templatestructis_reference_wrapper>:true_type{};然后你可以用它来消除歧义:templatevoiddo_stuff(T&&t,false_type){coutvoiddo_stuff(T&&r

c++ - std::merge 不适用于 std::async

我想在一个单独的线程中合并两个vectorintmain(){vectora(100);vectorb(100);vectorc(200);std::async(std::launch::async,std::merge,a.begin(),a.end(),b.begin(),b.end(),c.begin());}这不编译main.cpp:Infunction‘intmain()’:main.cpp:17:25:error:nomatchingfunctionforcallto‘async(std::launch,,std::vector::iterator,std::vector:

c++ - 为什么 std::cout 如此耗时?

我编写了一个程序来计算8个字符的字符串“sharjeel”的排列。#include#includecharstring[]="sharjeel";intlen=8;intcount=0;voidswap(char&a,char&b){chart=a;a=b;b=t;}voidpermute(intpos){if(pos==len-1){std::cout如果我打印每个排列,则大约需要9.8秒才能完成执行。40314lshaerej40315lshareej40316lshareje40317lshareej40318lshareje40319lsharjee40320lsharjeeP

c++ - 为什么 std::apply 会因泛型函数而失败?

取自cppreference,为什么调用std::apply(add_generic,...)编译失败?有办法解决吗?#include#includeintadd(intfirst,intsecond){returnfirst+second;}templateTadd_generic(Tfirst,Tsecond){returnfirst+second;}intmain(){std::cout它fails有错误:[x86-64gcc7(snapshot)]error:nomatchingfunctionforcallto'apply(,std::tuple)'[x86-64gcc7(s