我明白自动意味着类型扣除.我从来没有看到它被用作auto&
,而且我不明白:
在这个短代码中做了什么.
#include <iostream>
#include <vector>
#include <thread>
void PrintMe() {
std::cout << "Hello from thread: " << std::this_thread::get_id() << std::endl;
}
int main() {
std::vector<std::thread> threads;
for(unsigned int i = 0; i < 5; i++) {
threads.push_back(std::thread(PrintMe));
}
for(auto& thread : threads) {
thread.join();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我猜这是替代的某种合成糖
for(std::vector<std::thread>::iterator it = threads.begin(); it != threads.end(); it++ ) {
(*it).join();
}
Run Code Online (Sandbox Code Playgroud)
但我不明白这种语法是如何工作的,以及那个&符号在那里做了什么.
我正在进行一项练习,似乎更多地关注如何以数学方式处理问题,而不是语法上.
当数字相对较小时,这个想法很简单.给定基数和幂,程序应该对结果的数字求和.让我们用一个例子来解释我想做什么.
base 2 and power 8
因此
2^8 = 256
,程序应该对答案的数字进行求和
2+5+6 = 13
,这就是整点,以找到提升到幂的基数结果的数字之和.
现在,这是一个简单的例子,如果我移动到一个非常庞大的数字,让我们说2 ^ 1000,这几乎是不可能只是扔进我尝试过的任何东西,因为我们失去了精度,因为结果是巨大的并且被截断.答案必须准确.
我想也许有一种数学方法可以不同地做到这一点,不知何故将它分解成更小的块,但实际上我想不出除了以下之外的任何关系:
2^1000 = 2^500*2^500
1000 log(2) = log(ans)
无论哪种方式,这不会让我接近结果中的数字,我可以开始求和.
我希望我能清楚地解释清楚.
对于它的价值,我正在使用C++(也给了lua一个镜头)也许我可以使用一个库,但这个数字将有302个数字位,我应该编写我的程序来处理1000的指数.我真的认为我我在这里错过了一些数学技巧.
找到EDIT部分解决方案
我今天花了一点时间和lua试图解决这个问题,今晚我会用C++来试一试,看看我是否得到了不同的结果.我可以找到错误的来源,但我找到了适用于大多数情况的解决方案.只是不是2 ^ 1000,而是其他一些数字非常大的指数.
所描述的解决方案和来自MooseBoys的评论.
我使用模幂运算.lua代码如下:
-- Function purpose: Calculate modular exponent (see wiki in comment
from MooseBoys)
--
-- @param b: base
-- @param e: exponent
-- @param m: modulation
-- @result c: result
--
-- example: 2^15 = 32768
-- ModPow(2,15,10) = 8
-- ModPow(2,15,100) = 68
-- …
Run Code Online (Sandbox Code Playgroud) 我似乎无法找到答案,但也许我正在寻找错误的术语.我没有在点击中找到我正在寻找的答案.
我有一堆菜单系统的派生类.
我有一个CControl
派生类,它是CEditBox
一个CLabel
类和一个类的父类.CLabel
只不过是将文本附加到SDL_Surface上,然后将其绑定到用于渲染的openGL的纹理. CEditBox
将是一个用于显示文本或从用户收集文本的字段,如密码框.显然,CEditBox可以使用标签来处理框内的文本呈现. CControl
源于CComponent
.
我不能声明CLabel
里面,CEditBox
除非我包含CLabel
在标题中,但我认为我继续得到链接器错误,尽管我的所有标题都包含在#ifndef #define class #endif
语法中,但我也是一个菜鸟.相反,我有一个CComponent*
指针声明,因为它们是从该类派生的.
精细.现在在CEditBox
我的构造函数中:
#include "CLabel.h" //include in .CPP is fine I reckon.
CEditBox::CEditBox() {
CLabel Field; //Create CLabel
InputType = ALL; //Not important for my question related to allowed symbols
Label = &Field; //CComponent pointer to CLabel
}
Run Code Online (Sandbox Code Playgroud)
当这个构造函数返回时,CLabel不会超出范围,因此Feild会被销毁,现在我的指针指向一个未定义的内存块吗?什么是适当的方法来做到这一点?有更好的解决方案吗?
谢谢
我不知道问题已经存在,但有些人认为这是一个更重要的问题.那么现在是实际的代码,你们可以告诉我你是否认为它做错了.基类CMenuObject
#ifndef _CMENUOBJECT_H_
#define _CMENUOBJECT_H_
class CMenuObject { …
Run Code Online (Sandbox Code Playgroud) 首先,我发现这个答案特别有用.然而,它让我想知道如何找到这样的信息.
我似乎无法弄清楚如何迭代收件箱中的所有邮件.我目前使用的解决方案Uri.parse("content://mms-sms/conversations")
是使用"_id"和"ct_t".然而,似乎我只能在手机中找到三个对话,尽管有30个ms(其中20个在保存对话线程中,其他对话分为两个其他对话).对这样的陈述有意义content://mms-sms/conversations
.但是,其他提供商似乎只处理短信或彩信.有没有办法以这种方式迭代整个消息列表,我"content://mms-sms/conversations"
用其他东西替换?
public boolean refresh() {
final String[] proj = new String[]{"_id","ct_t"};
cursor = cr.query(Uri.parse("content://mms-sms/conversations"),proj,null,null,null);
if(!(cursor.moveToFirst())) {
empty = true;
cursor.close();
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我用下一个函数迭代消息
public boolean next() {
if(empty) {
cursor.close();
return false;
}
msgCnt = msgCnt + 1;
Msg msg;
String msgData = cursor.getString(cursor.getColumnIndex("ct_t"));
if("application/cnd.wap.multipart.related".equals(msgData)) {
msg = ParseMMS(cursor.getString(cursor.getColumnIndex("_id")));
} else {
msg = ParseSMS(cursor.getString(cursor.getColumnIndex("_id")));
}
if(!(cursor.moveToNext())) {
empty = true;
cursor.close();
return false;
}
return true; …
Run Code Online (Sandbox Code Playgroud) 我已经浏览了一些线程教程,但我很好奇一件事.
std::thread Child([](){ std::cout << "When am I executed?" << std::endl << std::endl; });
//Some other code
Child.join();
//I'm guessing now my thread would be running
Run Code Online (Sandbox Code Playgroud)
线程是在我调用时执行join()
还是在创建线程和调用join之间的某个时间运行?如果在join()
调用时执行它,只是为了检查我的理解,它告诉执行的部分,你的程序继续在主线程上,最终子线程在主线程正在使用的相同内存上做了一些工作?
如果我想为泛型类创建一个包装器,我会想做类似下面的事情,但我似乎无法完全弄明白.我对在内存方面管理线程很困惑.
class Sync {
private:
int val;
public:
Sync() : val(0) {}
void Inc() {val++}
int Value() { return val; }
};
class Async {
private:
Sync Foo;
std::mutex mtx;
std::vector<std::thread*> Children;
public:
//I would need a new thread each time I called Inc
void Inc() {
Children.push_back(new std::thread([&]() …
Run Code Online (Sandbox Code Playgroud) 以下代码出错。继续查看示例并重新评估代码,但无法真正发现任何不应该运行的原因。是否有任何错误'mRRed'
导致无法找到布局?(我确定后续)...
import tkinter as tk
from tkinter import ttk
class MainFrame:
def __init__(self,parent):
self.frame = ttk.Frame(parent,padding='3 3 12 12')
self.frame.grid(column=0, row=0)
self.mRRed = ttk.Style()
self.mRBlue = ttk.Style()
self.mPurple = ttk.Style()
self.mPink = ttk.Style()
self.mSCyan = ttk.Style()
self.mVYellow = ttk.Style()
self.mGreen = ttk.Style()
self.mRRed.configure("mRRed",background="#E61E50")
self.mRBlue.configure("mRBlue",background="#0F69AF")
self.mPurple.configure("mPurple",background="#503291")
self.mPink.configure("mPink",background="#EB3C96")
self.mSCyan.configure("mSCyan",background="#2BDECD")
self.mVYellow.configure("mVYellow",background="#FFC832")
self.mGreen.configure("mGreen",background="#149B5F")
self.toolRibbon = ttk.Frame(self.frame, style='mRRed')
self.subtoolRibbon = ttk.Frame(self.frame, style='mRBlue')
self.titleFrame = ttk.Frame(self.frame, style='mPruple')
self.contentFrame = ttk.Frame(self.frame, style='mPink')
self.optionRibbon = ttk.Frame(self.frame, style='mSCyan')
self.statusFrame = ttk.Frame(self.frame, style='mVYellow')
self.infoFrame = ttk.Frame(self.frame, style='mGreen')
self.toolRibbon.grid(column=0,row=0) …
Run Code Online (Sandbox Code Playgroud) 我用memcpy复制一个Vertex
由glm :: vec3对象组成的结构.它可以在类函数中复制结构.它在该函数返回类对象时调用的复制构造函数中不起作用.
为什么?
类函数返回对象
ShapeData ShapeGenerator::drawTriangle() {
ShapeData ret;
Vertex verts[] = {
glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3(1.0f, 0.0f, 0.0f),
glm::vec3(-1.0f, -1.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3(1.0f, -1.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f),
};
ret.numVerts = NUM_ARRAY_ELEMENTS(verts);
ret.verts = new Vertex[ret.numVerts];
memcpy(ret.verts, verts, sizeof(verts)); //WORKS
GLushort indicies[] = {0,1,2};
ret.numIndicies = NUM_ARRAY_ELEMENTS(indicies);
ret.indicies = new GLushort[ret.numIndicies];
memcpy(ret.indicies, indicies, sizeof(indicies));
return ret;
}
Run Code Online (Sandbox Code Playgroud)
复制构造函数
ShapeData(const ShapeData& data) {
verts = new Vertex[data.numVerts];
//memcpy(verts, data.verts, sizeof(data.verts)); //DOES NOT WORK
std::copy( data.verts, …
Run Code Online (Sandbox Code Playgroud) 在C++中,可以实现ostream operator <<
能够定义如何将类输出到流cout << Class
.
java中有可能做类似的事情out.println(Class)
吗?