我试图产生当前线程:
的std :: this_thread ::收率();
但不幸的是GCC知道的更好:
'yield'不是'std :: this_thread'的成员
我忘了一些类似于D_GLIBCXX_USE_NANOSLEEP的黑客攻击,或者是什么?
我正在使用JavaScript的Math.random()功能在桶上分配项目.然后,我在画布中显示桶.我希望这些项目能够均匀分布,但是(即使在多个浏览器中多次重试之后),看起来左边的分布更接近细粒度(接近于零)并且向右变得更均匀(接近1) ).请参见下图
.
我做错了,还是JavaScript的随机功能很糟糕?以下是用于生成此图像的代码:
<html>
<head>
<script>
window.onload = function() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var width = canvas.width;
var height = canvas.height;
var buckets = width;
var total = width*height*0.3;
var bucketList = [];
// initialized each bucket to 0 items
for(var i=0; i<buckets; ++i) {
bucketList[i] = 0;
}
// distribute all items over the buckets
for(var i=0; i<total; ++i) {
++bucketList[Math.floor(Math.random()*buckets)];
}
// draw the buckets
ctx.fillStyle = "rgb(200,0,0)";
for(var i=0; …Run Code Online (Sandbox Code Playgroud) 我很困惑为什么下面的C++代码可以编译.为什么调用删除0的方法不会产生任何错误?!
int *arr = NULL; // or if I use 0, it's the same thing
delete arr;
Run Code Online (Sandbox Code Playgroud)
我确实尝试过运行它,它根本没有给我任何错误......
如何计算小于值x的最大素数?
实际上,它不必是精确的,只是近似的并且接近于x.
x是32位整数.
想法是x是配置参数.我使用小于x的最大素数(称为y)作为类构造函数的参数.值y必须是素数.
我正在尝试从Boost Gzip过滤器页面编译示例:
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
int main()
{
using namespace std;
ifstream file("hello.gz", ios_base::in | ios_base::binary);
filtering_streambuf<input> in;
in.push(gzip_decompressor());
in.push(file);
boost::iostreams::copy(in, cout);
}
Run Code Online (Sandbox Code Playgroud)
可悲的是,我的g ++返回错误:
gzlib.cpp: In function ‘int main()’:
gzlib.cpp:12:3: error: ‘filtering_streambuf’ was not declared in this scope
gzlib.cpp:12:23: error: ‘input’ was not declared in this scope
gzlib.cpp:12:30: error: ‘in’ was not declared in this scope
gzlib.cpp:13:29: error: ‘gzip_decompressor’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
这个函数有什么问题以及如何修改它以使其工作?非常感谢!
链接到Boost Gzip过滤器:http://www.boost.org/doc/libs/release/libs/iostreams/doc/classes/gzip.html
我想知道在C++中使用'this'是否是一个好习惯.起初我以为我应该这样,因为你明确表示你所引用的东西是当前类的成员,但有时你会用以下代码结束:
Document::Document(QWidget *parent) : QWidget(parent)
{
this->file = 0;
this->layout = new QGridLayout(this);
this->layout->setSpacing(2);
this->layout->setMargin(0);
this->setLayout(this.layout);
this->textArea = new QTextEdit(this);
this->textArea->setLineWrapMode(QTextEdit::NoWrap);
this->textArea->setAcceptDrops(true);
this->textArea->setAcceptRichText(true);
this->textArea->setUndoRedoEnabled(true);
this->textArea->setFont(QFont("Mono" , 11));
this->layout->addWidget(this->textArea);
this->textArea->show();
this->textArea->setFocus();
}
Run Code Online (Sandbox Code Playgroud)
我认为如果没有使用所有'this',这样会更好看this->layout->addWidget(this.textArea);.我认为代码在大多数情况下应该使用相同的样式,以便更容易阅读,所以我应该在必要时使用"this",或者使用它来表明你引用的是一个好的做法同一类的成员.
我经常看到它认为不应该使用重度嵌套的函数调用,因为它们是不可读的.但是,使用临时变量会产生大量不必要的冗长,并迫使读者在心理上将每个临时变量链接到它所代表的内容.在查看Lisp代码通常被格式化的方式时,我发现嵌套函数调用实际上可以被设置为非常可读,如果您将它们格式化以反映嵌套.例如:
// Totally unreadable:
auto linesIter = filter!"a.length > 0"(map!strip(File(filename).byLine())))
// Perfectly readable. The only difference is formatting.
auto linesIter = filter!"a.length > 0"(
map!strip(
File(filename).byLine()
)
);
// Readable, but unnecessarily verbose:
auto rawLines = File(filename).byLine();
auto stripped = map!strip(rawLines);
auto filtered = filter!"a.length > 0"(stripped);
Run Code Online (Sandbox Code Playgroud)
在嵌套函数形式中编写类似第一个示例的东西是,恕我直言,相当于在更多程序风格的代码中执行以下操作:
for(i = 0; i < 10; i++) { for(j = 0; j < 10; j++) { if(x < 2) { z++; } else { y++; }}}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,真正的问题是格式不佳,而不是过度嵌套.您如何评价格式良好的嵌套函数版本与临时变量版本的可读性/可理解性?您认为重函数调用嵌套是不好的样式,即使它的格式是为了最大可读性?如果是这样,为什么?
我最近查看了一些代码并发现了类似于以下内容的数据结构:
class TreeNode {
std::vector<TreeNode> subNodes;
};
Run Code Online (Sandbox Code Playgroud)
如您所见,在定义TreeNode之前,使用TreeNode实例化容器.代码在GCC和MSVC下编译,但我记得看到有人说这不是保证行为.不幸的是,我根本无法在标准中找到任何内容.
这些容器如何实施?标准是否保证了这种行为?如果标准不能保证这一点,我对这种设计有什么选择?
我已经下载了Divx插件(我认为)html文件在我的所有其他浏览器中播放但是Chrome.相反,Chrome中显示的所有内容都是一个带有Divx字样的黑盒子.为什么?
这是我的HTML5代码:
<div class = "video-js-box">
<video class = "video-js" width = "675" height = "380" controls>
<source src = "/videos/videofiles/Reversing_a_Track.mp4"
type = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';/>
<source src = "/videos/videofiles/Reversing_a_Track.ogg"
type = 'video/ogg; codecs="theora, vorbis"'/>
<source src = "/videos/videofiles/Reversing_a_Track.webm"
type = 'video/webm; codecs="vp8, vorbis"'/>
</video>
</div>
Run Code Online (Sandbox Code Playgroud) 我是一名使用 C++ 代码的 Java 程序员,需要一些异常处理方面的帮助。
我有以下结构的代码:
try{
...
}
catch( ... )
{
log("Exception occurred");
}
Run Code Online (Sandbox Code Playgroud)
发生了异常,但 try 块确实很大,而且调试不是一种选择,因此我需要最少地修改代码以提供有关异常的相关信息。
所以我在现有的 catch 块之前添加了以下 catch 块(使用我的 Java 知识并参考 C++ 指南):
catch(exception e)
{
log(e.what());
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然收到旧消息 - “发生异常”。try 块中的代码包含许多低级函数,例如strncpy, memcpy等。
为什么这个catch块没有按预期工作?我该怎么做才能获取有关正在发生的异常以及在哪一行上的信息,类似于 Java 轻松提供的堆栈跟踪。
c++ ×6
coding-style ×2
algorithm ×1
c++11 ×1
canvas ×1
containers ×1
d ×1
distribution ×1
divx ×1
gcc ×1
gzip ×1
html5 ×1
html5-video ×1
javascript ×1
null ×1
primes ×1
random ×1
readability ×1
recursion ×1
try-catch ×1
windows-7 ×1
zlib ×1