我使用了以下语法糖:
for (auto& numberString: {"one", "two", "three", "four"}) { /* ... */}
Run Code Online (Sandbox Code Playgroud)
这是有效的代码吗?AFAIK,基于这个问题,这应该是非法的,但代码按预期运行。我认为我对此事的理解不正确。
据我所知,只有文字不应该有内存地址,但链接的问题正在讨论临时值和右值。
我收到以下错误:
FAILURE: Build failed with an exception.
* Where:
Initialization script 'C:\Users\User\AppData\Local\Temp\Console_main__.gradle' line: 18
* What went wrong:
A problem occurred configuring root project 'demo'.
> Could not create task ':Console.main()'.
> Unnecessarily replacing a task that does not exist is not supported. Use create() or register() directly instead. You attempted to replace a task named 'Console.main()', but there is no existing task with that name.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info …Run Code Online (Sandbox Code Playgroud) 鉴于lambda的类型是可确定的(可强制转换为std::function(如果我错了,请纠正我)),重载的函数应该同时使用两个函子。已定义?([&]() -> Type {})
请注意,对于我当前的解决方案,我需要按引用捕获,这就是为什么代码包含其逻辑的原因。
以下示例描述了该问题:
#include <iostream>
#include <string>
#include <functional>
void do_some(std::function<void(int)> thing)
{
thing(5);
}
void do_some(std::function<bool(int)> thing)
{
if (thing(10))
{
std::cout << "it's true!" << std::endl;
}
}
int main()
{
int local_to_be_modified = 0;
do_some(
[&](int in)
{
local_to_be_modified = in;
std::cout << "This is void-" << std::endl;
}
);
do_some(
[&](int in) -> bool
{
// error: call to 'do_some' is ambiguous
local_to_be_modified += in;
std::cout << "This is …Run Code Online (Sandbox Code Playgroud) 我写了一个2D平台游戏,我需要有(最多4个)门的房间.我用Java编写它,但语言无关紧要.
每个房间可以有4个门,在顶部,底部和侧面.我打电话给他们NORTH,SOUTH,EAST和WEST.当我正在建造一个房间时,我只给它一个整数,整数中的每个位代表一个门.
例如,如果我想要一个有3个门的房间(一个在北方,一个在东方,在西方)我给房间号码:11(1011二进制).
出于这个原因,每扇门都有一个整数,标识它.
NORTH = 8;//1000
SOUTH = 4;//0100
EAST = 2;//0010
WEST = 1;//0001
Run Code Online (Sandbox Code Playgroud)
如果我生成一个房间,我会给它们这些标识符的组合.
例如:前面提到的房间将获得标识符
doorStock = NORTH | EAST | WEST;
Run Code Online (Sandbox Code Playgroud)
我将这些门存放在一个简单的阵列中:
Door doors[] = new Door[4];
Run Code Online (Sandbox Code Playgroud)
我的问题是:我需要一个能够将标识符映射到数组中正确索引的函数.我并不总是需要4扇门.
我最初做的似乎是最简单的:门数组总是有4个元素,而我不会使用的索引只是保持为空.
public Door getDoor(int doorID){
switch(doorID){
case NORTH:{
return doors[0];
}
case SOUTH:{
return doors[1];
}
case EAST:{
return doors[2];
}
case WEST:{
return doors[3];
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
为了安全起见,我需要确定我要求的门是否确实存在于房间内.
private boolean doorExists(int doorID){
return (doorID & doorStock) != …Run Code Online (Sandbox Code Playgroud) 我尝试用Qt 5.5.1构建一个简单的OpenGL应用程序,一切都很好,直到我尝试使用像glClearColor这样的openGL本机函数调用.
Widget实际上编译并生成黑屏,但在我尝试使用任何openGL本机函数之后它甚至没有链接,但会产生错误:
glwidget.cpp:10: error: undefined reference to '_imp__glClearColor@16'
这是.pro文件:
QT += core gui opengl
CONFIG += windows
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Vehicle_simulation
TEMPLATE = app
SOURCES += main.cpp\
simulation.cpp \
glwidget.cpp
HEADERS += simulation.h \
glwidget.h
FORMS += simulation.ui
Run Code Online (Sandbox Code Playgroud)
我正在使用Desktop Qt mingw492_32套件.奇怪的是,我没有在lib文件夹中找到libQtOpenGL.so.我的QT安装有问题吗?我试图多次重新安装它,但它没有帮助.我在哪里可以下载该特定库?
将它链接到项目将解决问题,但我似乎无法在任何地方找到它.
问题是QT安装中缺少openGL模块,并不是我无法链接到它.
从定义为具有精确N元素的 RepeatedField 中,我正在填充一个 std::array 。
std::array<int, 6> entry;
size_t i = 0;
for (const auto& item: msg.items()){
assert(i < 6);
entry[i++] = item;
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但是有更好的方法来初始化它吗?填充 std::vector 可以很好地处理初始化列表,但 std::array确实不同。
是否可以在包含 的结构体的聚合初始化中使用 RepeatedFields std::array?
到目前为止我所尝试的甚至无法编译:
entry = {msg.items().begin(), msg.items().end()};
Run Code Online (Sandbox Code Playgroud)
这是可以理解的,因为 std::array 没有接受std::initializer_list;的构造函数。但我正在尝试做的事情可能吗?
根据这个问题,find和之间有很大的区别matches(),仍然以某种形式提供结果。
作为一种实用程序,该toMatchResult函数返回matches()操作的当前结果。我希望我的假设(1)是有效的。(正则表达式在这里)
String line = "aabaaabaaabaaaaaab";
String regex = "(a*b)a{3}";
Matcher matcher = Pattern.compile(regex).matcher(line);
matcher.find();
// matcher.matches();(1) --> returns false because the regex doesn't match the whole string
String expectingAab = matcher.group(1);
System.out.println("actually: " + expectingAab);
Run Code Online (Sandbox Code Playgroud)
不幸的是,以下方法无效(例外:未找到匹配项):
String line = "aabaaabaaabaaaaaab";
String regex = "(a*b)a{3}";
String expectingAab = Pattern.compile(regex).matcher(line).toMatchResult().group(1);
System.out.println("actually: " + expectingAab);
Run Code Online (Sandbox Code Playgroud)
这是为什么?我的第一个假设是它不起作用,因为正则表达式应该匹配整个字符串;但同样的异常也与字符串值一起抛出aabaaa......
当然,匹配器需要使用 设置为正确的状态find(),但是如果我想使用 oneliner 呢?我实际上为此实现了一个实用程序类:
protected static class FindResult{ …Run Code Online (Sandbox Code Playgroud) 我已经用相机设置了一个场景
cam = new PerspectiveCamera(80, w, h);
cam.position.set(0, 0, -1);
cam.lookAt(0, 0, 0);
cam.update();
Run Code Online (Sandbox Code Playgroud)
但是有些场景丢失了,因为它超出了相机的视距.
可以使用函数的指针返回,这在很多方面都很有用,但是否建议从该返回值中获取引用?
#include <iostream>
#include <memory>
using namespace std;
unique_ptr<int> give_a_number(){
return std::make_unique<int>(6);
}
int main(){
int& ret = *give_a_number();
return ret;
}
Run Code Online (Sandbox Code Playgroud)
根据消毒剂,没有发生泄漏:
user@comp: Scrapbook$ g++ main.cpp -fsanitize=address -g
user@comp: Scrapbook$
Run Code Online (Sandbox Code Playgroud)
所以参考以某种方式被清理,但我不明白为什么。清理是如何发生的,甚至 unque_ptr 如何能够跟踪数据。这应该被视为安全行为吗?这是社区中公认的用法吗?
在下面的代码中:
#include <iostream>
#include <vector>
int main()
{
std::cout<<"Hello World";
std::vector<std::vector<int>> v;
while(v.size() <= 2){
v.insert(v.begin(),{}); //1
std::cout << "!";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
每次迭代的输出都变得越来越激进,因为尽管进行了操作v.size(),但输出从未增加insert。
但是,当initializer_list其中有元素或被临时元素替换时,循环会按预期运行多次。
...
v.insert(v.begin(),{0}); //1
...
Run Code Online (Sandbox Code Playgroud)
...
v.insert(v.begin(),std::vector<int>()); //1
...
Run Code Online (Sandbox Code Playgroud)
这是为什么?如果隐式转换失败,不应该出现编译错误吗?
我想尽快将一个向量复制到另一个向量中。
一种方法是使用std::copy:
std::copy(other_vector().begin(),other_vector().end(),this_vector.begin());
Run Code Online (Sandbox Code Playgroud)
但由于向量很长,我想知道该std::copy函数是否已实现,因此它会使用多个线程。
我可以提供一个自定义逻辑,用于将向量划分为相等的分离部分,以分别复制项目,但我不想再次重新发明轮子。
那么可以std::copy用于多线程吗?