在测试某些功能的同时std::thread,一位朋友遇到了GCC的问题,我们认为值得问一下这是否是GCC错误或者这个代码可能有问题(代码打印(例如)"7 8 9 10 1 2 3" ,但我们希望打印[1,10]中的每个整数:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <thread>
int main() {
int arr[10];
std::iota(std::begin(arr), std::end(arr), 1);
using itr_t = decltype(std::begin(arr));
// the function that will display each element
auto f = [] (itr_t first, itr_t last) {
while (first != last) std::cout<<*(first++)<<' ';};
// we have 3 threads so we need to figure out the ranges for each thread to show
int increment = std::distance(std::begin(arr), std::end(arr)) / 3;
auto first …Run Code Online (Sandbox Code Playgroud) 我正在尝试将旧的makefile代码转换为CMake.你能帮助我吗?这是我目前陷入困境的部分.我不知道如何将这些参数传递给编译器.
COMPILE_FLAGS = -c -m32 -O3 -fPIC -w -DSOMETHING -Wall -I src/sdk/core
ifdef STATIC
OUTFILE = "bin/test_static.so"
COMPILE_FLAGS_2 = ./lib/ABC.a
else
OUTFILE = "bin/test.so"
COMPILE_FLAGS_2 = -L/usr/lib/mysql -lABC
endif
all:
g++ $(COMPILE_FLAGS) src/sdk/*.cpp
g++ $(COMPILE_FLAGS) src/*.cpp
g++ -fshort-wchar -shared -o $(OUTFILE) *.o $(COMPILE_FLAGS_2)
rm -f *.o
Run Code Online (Sandbox Code Playgroud)
谢谢!
根据文件它:
当且仅当容器中没有具有等效键的元素时,才在容器中插入使用参数args构造的对象.
但是,可以插入到unordered_map中的唯一对象具有类型 std::pair<Key const, Mapped>(因为要插入的对象需要键和值),这已知具有正好两个参数的构造函数.那么为什么它使用可变函数形式呢?当然,我完全不了解这一点.
shared_ptr的相等运算符定义如下:
template<class T, class U> inline bool operator==(
shared_ptr<T> const & a, shared_ptr<U> const & b)
{
return a.get() == b.get();
}
Run Code Online (Sandbox Code Playgroud)
这似乎破了.把这种平等转移到a和b所指的是不是更好?或者这是对图书馆用户的不公平限制(因为他们必须提供一个平等运营商)?
如果我有一个包含shared_ptrs的map或hash_table,那么当前定义会使等式无法使用.例如,考虑一下
std::map<int, std::tr1::shared_ptr<T> > m1, m2;
Run Code Online (Sandbox Code Playgroud)
我们不想检查m1和m2中每个int的ptrs是否指向相同的值?
我可以通过展平m1,m2 out来实现我自己的相等性(从每个构造集合,沿途取消引用shared_ptrs).是否有一个STL技巧可以完成这个或其他方式在shared_ptrs存在的情况下整齐地测试相等性?
我正在尝试学习wxWidgets,但我仍然坚持一个我无法在文档中找到解释的地方.我想了解这个最小的wxWidgets程序:
#include <wx/wx.h>
class MyApp : public wxApp
{
virtual bool OnInit();
};
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
wxFrame *frame = new wxFrame(NULL, -1, _("Hello World"), wxPoint(50, 50),
wxSize(450, 350));
frame->Show(true);
return true;
}
Run Code Online (Sandbox Code Playgroud)
具体来说,为什么frame不泄漏?什么时候发布,谁的责任是?在一个普通的程序中,一个没有被传递给任何东西并且没有被删除而超出范围的指针几乎肯定是泄漏,但显然在wxWidgets中并非如此.
我有几个关于java中垃圾收集器的问题.
Q1.据我所知,当对象超出范围且JVM即将收集垃圾时,会调用finalize().我认为垃圾收集器会自动调用finalize()方法,但在这种情况下它似乎不起作用.解释是什么?为什么我需要显式调用finalize()方法?
public class MultipleConstruct {
int x,y;
public MultipleConstruct(int x)
{
this.x= x;
y=5;
System.out.println("ONE");
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
System.out.println("FINALIZED");
}
public static void main(String[] args) throws Throwable {
MultipleConstruct construct = new MultipleConstruct(3);
}
}
Run Code Online (Sandbox Code Playgroud)
Q2.另外,什么时候调用垃圾收集器?我理解gc是一个守护程序线程,由JVM根据剩余的堆大小调用.这是否意味着,JVM等待程序使用资源的阈值限制,然后通知gc扫描垃圾对象.
编辑: gc如何解决循环引用?
根据每一个源我发现,以手动标记在谷歌Analytics(分析)需要3场竞选跟踪链接- utm_campaign,utm_source和utm_medium.但是,当我不使用后两者时,我得到了混合的结果.
如果我设置utm_campaign和utm_source,竞选是GA跟踪与中间值(not set).这似乎可靠地发挥作用.
如果我utm_campaign在另一个网站上设置锚标记的href并且遗漏了utm_source,则在使用GA调试脚本时,广告系列来源会显示为引荐域,但是当我查看时,没有列出广告系列名称,也没有正确跟踪Google Analytics中的报告.例如,如果在www.referringsite.com上我有一个看起来像<a href="http://www.mysite.com?utm_campaign=test">click me</a>
调试工具将显示的锚,
Campaign Source: referringsite并且Campaign Name未列出参数.
这里发生了什么?我什么时候需要使用这些参数?何时可以将它们遗漏?我的最终目标是在utm_source未明确设置时允许引荐来源用作广告系列来源,并utm_medium完全省略(看来我可以毫无问题地执行).有人可以具体解释在何时何地使用什么,以及如何实现使用httpReferrer作为隐式广告系列来源的预期效果?
我有一个std :: vector,我想检查每个元素的特定属性.SomeStruct有一个属性'type'.我想检查此属性是Type1还是Type2.
我的计划是使用boost :: lambda.
std::vector<SomeStruct>::const_iterator it =
std::find_if(
vec.begin(), vec.end(),
_1.type == SomeStruct::Type1 || _1.type == SomeStruct::Type2);
Run Code Online (Sandbox Code Playgroud)
因为我需要访问每个元素的特定属性,所以我不确定是否可以使用boost :: lambda.
任何提示?
我正在寻找一种初始化复杂结构的方法,该结构在一行中包含向量.
例如,如果我有这个结构:
struct A{
double x;
double y;
};
struct B{
double z;
double W;
};
struct C{
A a;
B b;
};
Run Code Online (Sandbox Code Playgroud)
我可以用这种方式初始化C: C c = {{0.0,0.1},{1.0,1.1}};
但是如果我必须初始化这样的结构呢?
struct A{
double x;
double y;
};
struct B{
vector<double> values;
};
struct C{
A a;
B b;
};
Run Code Online (Sandbox Code Playgroud)
我必须在一行中完成它,因为我想允许我的应用程序的用户在单个字段中指定所有初始值.当然,我更喜欢采用标准方式来做而不是自定义方式.
谢谢!
我正在使用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)