是否可以检查a std::future是否已经完成?据我所知,唯一的方法就是调用wait_for零持续时间并检查状态是否ready存在,但是有更好的方法吗?
如果我有一个迭代到载体a,然后我布展构建体或移动指派矢量b从a,是否仍迭代器指向同一元件(现在矢量b)?这就是我在代码中的意思:
#include <vector>
#include <iostream>
int main(int argc, char *argv[])
{
std::vector<int>::iterator a_iter;
std::vector<int> b;
{
std::vector<int> a{1, 2, 3, 4, 5};
a_iter = a.begin() + 2;
b = std::move(a);
}
std::cout << *a_iter << std::endl; // Is a_iter valid here?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否a_iter仍然有效的,因为a已移入b,或者是通过招无效的迭代器?作为参考,std::vector::swap 不会使迭代器无效.
object a = new Dog();
Run Code Online (Sandbox Code Playgroud)
VS
Dog a = new Dog();
Run Code Online (Sandbox Code Playgroud)
在两种情况下a.GetType()给出Dog.两者都调用相同的构造函数(具有相同的层次结构).
那么请你告诉我这两个陈述之间的区别吗?
我使用了C++ 11标准提供的新的基于范围的for循环,我提出了以下问题:假设我们vector<>使用基于范围的迭代迭代for,并且我们在向量的末尾添加一些元素这个迭代.那么,什么时候循环结束?
例如,请参阅以下代码:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<unsigned> test({1,2,3});
for(auto &num : test) {
cout << num << " ";
if(num % 2)
test.push_back(num + 10);
}
cout << "\n";
for(auto &num : test)
cout << num << " ";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用"-std = c ++ 11"标志测试了G ++ 4.8和Apple LLVM版本4.2(clang ++),输出是(对于两者):
1 2 3
1 2 3 11 13
Run Code Online (Sandbox Code Playgroud)
请注意,第一个循环终止于原始向量的末尾,尽管我们向其添加了其他元素.似乎for-range循环仅在开始时评估容器结束.事实上,这是范围的正确行为吗?是否由委员会指定?我们能相信这种行为吗?
请注意,如果我们改变第一个循环
for(vector<unsigned>::iterator it = test.begin(); it != test.end(); ++it) …Run Code Online (Sandbox Code Playgroud) 可能重复:
常规强制转换与static_cast对比dynamic_cast
在C++中,为什么要使用static_cast <int>(x)而不是(int)x?
static_cast<float>(foo)和之间有什么区别float(foo)?我经常static_cast<float>在模板代码中看到或类似于从任何整数类型到某些特定的整数类型.例如,我想要一个函数在任何整数类型上执行浮点除法.我通常会看到这样的事情:
template <typename T>
float float_div(T lhs, T rhs)
{
// I normally see this
return static_cast<float>(lhs) / static_cast<float>(rhs);
// But I could do this
// return float(lhs) / float(rhs); // But I could do this
}
Run Code Online (Sandbox Code Playgroud)
使用float(lhs)或有任何优势static_cast<float>(lhs)吗?还有什么是float(lhs)初始化/转换的名称?
我正在尝试使用这个方法在这里提到的文件名之后命名一个lua包,但是_REQUIREDNAME从未定义过.例如,我有这两个文件
samplePackage.lua:
print("_REQUIREDNAME: ", _REQUIREDNAME)
return nil;
Run Code Online (Sandbox Code Playgroud)
packageTest.lua:
require "samplePackage"
Run Code Online (Sandbox Code Playgroud)
当我运行packageTest.lua时,它会输出 > _REQUIREDNAME: nil
我也在Lua 5.1 Refrence手册中找不到_REQUIREDNAME的提及,所以这是从语言中删除了,还是我遗漏了什么?
我认为Boost :: variant在1_54中被破坏了.我试图在boost变量中使用std :: unique_ptr作为有界类型.
根据1_54文档,变体需要是可复制的或Move Constructable.
http://www.boost.org/doc/libs/1_54_0/doc/html/variant/reference.html
所以我实现了移动构造函数并在我的代码中禁用了复制构造函数.
当我尝试将某些内容分配给变体对象时,它无法编译.我尝试了各种不同的东西,包括使用std :: move为变量对象分配数据,但似乎没有任何效果.在编译错误堆栈跟踪之后我确定问题出在variant.hpp中,它试图备份rhs数据.我想知道你们的想法,让我知道我是否正确地认为提升变体文档是错误的.
提前致谢.
我正在使用vs2010编译并使用C++ 11.
这是我的测试代码:
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include <string>
#pragma warning (push)
#pragma warning (disable: 4127 4244 4265 4503 4512 4640 6011)
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <boost/ref.hpp>
#include <boost/shared_ptr.hpp>
#pragma warning (pop)
#include <boost/foreach.hpp>
#include <boost/format.hpp>
using boost::format;
using boost::str;
using namespace std;
class UniqueTest
{
};
class Foo
{
public:
std::unique_ptr<UniqueTest> testUniquePtr;
Foo() { std::cout << "Foo::Foo\n"; }
Foo (Foo&& moveData) …Run Code Online (Sandbox Code Playgroud) 我正在使用Qt Creator并且努力使.exe文件默认以管理员身份运行.
通过在线阅读所有解决方案,我试着将这一行放在我的.pro文件中:
QMAKE_LFLAGS += /MANIFESTUAC:"level='requireAdministrator' uiAccess='false'"
Run Code Online (Sandbox Code Playgroud)
但是当我检查我的.exe(使用记事本)时,它包含:
<requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
Run Code Online (Sandbox Code Playgroud)
有人能告诉我,如何添加requireAdministrator?
临时解决方案:
直到现在我找不到解决方案所以我做了一个临时的黑客攻击.我做了一个.exe名为'LaunchAnother.exe',它将使用以下代码启动我的'main.exe':
SHELLEXECUTEINFO shExInfo = {0};
shExInfo.cbSize = sizeof(shExInfo);
shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExInfo.hwnd = 0;
shExInfo.lpVerb = _T("runas"); // Operation to perform
shExInfo.lpFile = _T("main.exe"); // Application to start
shExInfo.lpParameters = ""; // Additional parameters
shExInfo.lpDirectory = 0;
shExInfo.nShow = SW_SHOW;
shExInfo.hInstApp = 0;
if (ShellExecuteEx(&shExInfo))
{
WaitForSingleObject(shExInfo.hProcess, INFINITE);
CloseHandle(shExInfo.hProcess);
}
Run Code Online (Sandbox Code Playgroud)
还在等待更好的解决方案.
此示例程序获取另一个向量中包含的向量元素的迭代器.我向包含向量添加另一个元素,然后打印出以前获得的迭代器的值:
#include <vector>
#include <iostream>
int main(int argc, char const *argv[])
{
std::vector<std::vector<int> > foo(3, std::vector<int>(3, 1));
std::vector<int>::iterator foo_it = foo[0].begin();
std::cout << "*foo_it: " << *foo_it << std::endl;
foo.push_back(std::vector<int>(3, 2));
std::cout << "*foo_it: " << *foo_it << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于未修改向量对应,foo_it我希望迭代器仍然有效.但是,当我运行此代码时,我得到以下输出(也在ideone上):
*foo_it: 1
*foo_it: 0
Run Code Online (Sandbox Code Playgroud)
作为参考,我使用g ++版本4.2和4.6以及clang 3.1获得此结果.但是我用g ++使用-std=c++0x(ideone链接)得到了预期的输出,并且clang在使用-std=c++0x和时得到了 -stdlib=libc++.
我在这里以某种方式调用了一些未定义的行为吗?如果是这样,现在定义的行为C++ 11?或者这只是一个编译器/标准库错误?
编辑我现在可以看到,在C++ 03中,迭代器无效,因为在重新分配时复制了向量的元素.但是我仍然想知道这在C++ 11中是否有效(即向量的元素保证被移动而不是复制,并且移动向量而不是使它的迭代器无效).
如何将char向量传递给char *?我知道这个问题可以很容易地用一个带有 SIZE const 的预定义 char[] 数组来解决,但我想要一个向量的灵活性,因为没有预定义的大小。
using namespace std;
//prototype
void getnumberofwords(char*);
int main() {
//declare the input vector
vector<char> input;
/*here I collect the input from user into the vector, but I am omitting the code here for sake of brevity...*/
getnumberofwords(input);
//here is where an ERROR shows up: there is no suitable conversion from std::vector to char*
return 0;
}
void getnumberofwords(char *str){
int numwords=0;
int lengthofstring = (int)str.size();
//this ERROR says the expression must have a …Run Code Online (Sandbox Code Playgroud)