我有这门课
class XXX {
public:
XXX(struct yyy);
XXX(std::string);
private:
struct xxx data;
};
Run Code Online (Sandbox Code Playgroud)
第一个构造函数(使用结构)很容易实现.第二个我可以以特定格式分开一个字符串,解析并且我可以提取相同的结构.
我的问题是,在java中我可以这样做:
XXX::XXX(std::string str) {
struct yyy data;
// do stuff with string and extract data
this(data);
}
Run Code Online (Sandbox Code Playgroud)
使用this(params)调用另一个构造函数.在这种情况下,我可以类似的东西?
谢谢
鉴于n,我想找到我这样的phi(i) = n.n <= 100,000,000.最大值i = 202918035 for n = 99683840.我想解决这个问题
我的方法是预先计算所有数字的最大函数i.为此,我首先i使用筛选时代的方法找到所有素数达到最大值.在筛子时记录素数的总数.然后使用

然后我在phi数组中搜索输入数字并输出打印结果.但它超出了时间限制.什么可以在预先计算中进一步优化,或者有更好的方法来做到这一点?
我的代码是:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int* Prime = (int*)malloc(sizeof(int) * (202918036 >> 5 + 1));
int* pos = (int*)malloc(sizeof(int) * (11231540));
int* phi = (int*)malloc(sizeof(int) * 202918036);
#define prime(i) ((Prime[i >> 5]) & (1 << (i & (31))))
#define set(j) (Prime[j >> 5] |= (1 << (j …Run Code Online (Sandbox Code Playgroud) 当我查找Date对象的原型时,我得到一个奇怪的消息:
Date.prototype; // Invalid Date
Run Code Online (Sandbox Code Playgroud)
这很奇怪; 为什么我没有Date按预期从原型中获取对象?此外,它返回的消息是一个字符串,但typeof(Date.prototype)返回"object".我也觉得很特别.为什么我得到这个输出?
我可以写作
template< class T0> struct Last0
{
using type = decltype(T0{}); // OK compiles. `type = T0`
};
template< class T0, class T1> struct Last1
{
using type = decltype(T0{}, T1{}); // OK, compiles. `type = T1`
};
template< class T0, class T1, class T2> struct Last3{
using type = decltype(T0{}, T1{}, T2{}); // Ok, compiles. `type = T2`
};
Run Code Online (Sandbox Code Playgroud)
但是,当我使用可变参数模板时,它不会被编译:
template< class ... T> struct Last{
using type = decltype(T{} ... ); //<--- Error !!!
};
Run Code Online (Sandbox Code Playgroud)
什么问题?
我想直接读入一个代码如下的字符串:
std::string myString(( std::ostringstream() << myInt << " "
<< myFloat << " "
<< std::boolalpha << myBool ).str());
Run Code Online (Sandbox Code Playgroud)
但VS2012给了我一个basic_ostream没有str()方法的投诉.
有没有办法用匿名字符串流来做到这一点?
ISO C++ 11规范的第5.1.2节第10节规定:
使用通常的非限定名称查找规则(3.4.1)查找捕获列表中的标识符; 每个这样的查找应该找到一个变量,其自动存储持续时间在本地lambda表达式的到达范围内声明.如果实体(即变量或此实体)出现在lambda表达式的捕获列表中,则称其被明确捕获.
这似乎意味着lambda无法捕获文件范围变量.例如,该程序应该是非法的:
#include <iostream>
int x = 13;
int main()
{
auto l = [](){ return x; };
std::cout << l() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,g++4.7.1会产生我期望的结果:
$ g++ --version
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++11 lambda.cpp
$ ./a.out
13
Run Code Online (Sandbox Code Playgroud)
但clang …
我写了一个简短的程序来说明我学校项目的继承原则,但我遇到了一个奇怪的问题.这是我的代码:(我省略了所有不是问题的代码)
class Car
{
protected:
double fuelLevel;
public:
void fuelUp(double);
};
void fuelUp(double fuel)
{
Car::fuelLevel += fuel;
}
Run Code Online (Sandbox Code Playgroud)
这是构建日志:
||=== Build: Debug in wierdError (compiler: GNU GCC Compiler) ===|
||In function 'void fuelUp(double)':|
|4|error: 'double Car::fuelLevel' is protected|
|11|error: within this context|
|4|error: invalid use of non-static data member 'Car::fuelLevel'|
|11|error: from this location|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Run Code Online (Sandbox Code Playgroud)
我不知道这个错误意味着什么,我希望有人可以帮助我.
我之前问了一个问题,有人给我一个指南,我读了它,我看到了这个
var temp = setTimeout,
setTimeout = function() {};
Run Code Online (Sandbox Code Playgroud)
他说,由于JavaScript提升,temp将不确定,我不明白为什么不应该这样?
var temp;
temp = setTimeout;
setTimeout = function() {};
Run Code Online (Sandbox Code Playgroud)
为什么它未定义?
我知道你不能在不使用构造函数的情况下直接在类中初始化成员变量(静态常量除外)。
但只是想知道这背后的原因是什么。下面是代码片段
如果有任何机构可以提供帮助
class a
{
int c=5;
// giving error error C2864: 'a::c' : only static const integral data members can be
// initialized within a class
int b;
public:
a():c(1),b(2){}
void h()
{
printf("%d,%d",c,b);
}
};
int main()
{
a l;
l.h();
getchar();
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有私有属性的类,它是一个向量.做getter函数的最佳方法是什么?
vector<char*> getNames() { return names; }
vector<char*>::iterator getNames() { return names.begin(); }