我很好奇使用Big O Notation描述这个的官方方式是什么?
var prices = [100, 180, 260, 590, 40, 310, 535, 10, 5, 3];
var biggest_profit = 0;
for (var i=0; i < prices.length; i++) {
var first_price = prices[i];
for (var j=i+1; j <= prices.length; j++) {
// do something here
}
}
Run Code Online (Sandbox Code Playgroud)
这有点让我失望:
j=i+1
Run Code Online (Sandbox Code Playgroud)
每次我们经历i
,j
变得越来越短.
Big O Notation中此模式的正确名称是什么?
我想从中获取对象,set<Node>
但我认为我的函数给了我这个对象的副本.怎么解决?
Node findByNum(int n){
for (set<Node>::iterator it = this->children.begin();it != this->children.end(); it++){
if ((*it).num == n){
return (*it);
}
}
}
Run Code Online (Sandbox Code Playgroud) 在发布模式下使用Visual Studio调试本机C++程序有什么缺点?为什么我要通过设置两个不同模式而不是仅仅在发布模式下调试来打扰自己?
通过在发布模式下调试,我的意思是在设置所有需要的配置(生成调试信息,禁用优化和增量链接......)之后
换句话说,为什么我应该有一个调试模式,我必须将我的项目链接到任何第三方的调试版本,而我可以简单地将我的发布模式更改为"可调试版本的发布模式"(假设我不想调试我的应用程序时深入第三个库)
我有函数foo:
void foo(){
//Command set A
std::this_thread::sleep_for(100s);
//Command set B
}
Run Code Online (Sandbox Code Playgroud)
第一部分是Command set A
必须阻止执行.但是,该sleep
部分并//Command set B
不必阻止执行,也不会返回任何数据.
所以我实现如下:
void foo(){
//Command set A
std::thread t([](){
std::this_thread::sleep_for(100s);
//Command set B
}
t.detach()
}
Run Code Online (Sandbox Code Playgroud)
我detach
在这里使用得当吗?它是正确的使用场所detach
吗?有更好的解决方案吗?
我有两节课:
template <class T1, class T2>
class foo{
//something here
}
class bar{
bar()=delete;
bar(const foo& a): a(a){}
private:
foo a;
}
Run Code Online (Sandbox Code Playgroud)
此代码无效.
但是,我怎样才能实现这样的目标呢?
我知道我应该告诉我们使用的是什么类型foo
.但实际上,我不允许默认构造函数.所以,我不能让的对象bar
,而不给它合适的foo
.为什么我要告诉编译器什么是T1
和T2
呢?
我有这个:
class point{
public:
point()=default;
point(int x,int y):x(x),y(y){}
int x,y;
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
class quad{
public:
quad()=default;
quad(point a,point b,point c, point c):a(a),b(b),c(c),d(d){};
point a,b,c,d;
}
Run Code Online (Sandbox Code Playgroud)
总的来说,我可以这样做:
point a(0,0),b(1,1),c(2,2),d(3,3);
quad q(a,b,c,d);
Run Code Online (Sandbox Code Playgroud)
或直接这个:
quad q(point(0,0),point(1,1),point(2,2),point(3,3));
Run Code Online (Sandbox Code Playgroud)
但当然不是这个:
quad q(0,0,1,1,2,2,3,3); // I know it's wrong
Run Code Online (Sandbox Code Playgroud)
问题:
是否可以在不声明新构造函数的情况quad
下使用8个整数来使用最后一个代码?这个问题的动机是有效的方式emplace_back
.更清楚:
std::vector<point> points;
points.push_back(point(0,0)); // you have to pass object
points.emplace_back(0,0); // you have just to send the arguments of the constructor
Run Code Online (Sandbox Code Playgroud) 是否由标准保证如果std::mt19937
用相同的数字播种,它将在所有平台上产生相同的数字序列?
换句话说,它的实现是由标准定义好还是std::rand()
被认为是实现细节?
我有这门课:
class foo{
public:
const std::vector<int> get_v() const{
return v_;
}
private:
std::vector<int> v_
};
Run Code Online (Sandbox Code Playgroud)
我能这样用吗?
int main(){
foo f;
some_non_inplace_std_function(f.get_v().cbegin(),f.get_v().cend());
}
Run Code Online (Sandbox Code Playgroud)
第一个f.get_v()
指向第二个相同的向量f.get_v()
吗?
我有一节课让我们称之为Person
:
class Person{
private:
void move(x,y,z);
}
Run Code Online (Sandbox Code Playgroud)
我有另一个叫做的课PersonController
:
class PersonController{
public:
void control(){
while(some_thing){
//do some calculations
controlled_person_->move(some_values); //Wrong Accessing to a private member
}
}
private:
Person* controlled_person_;
}
Run Code Online (Sandbox Code Playgroud)
这两个Person
和PersonController
是我设计的库的公共接口的一部分.
我希望PersonController
能够打电话move
给Person
.但是,我不希望任何人move
从公共接口访问此函数().
解决问题的简单方法是添加友谊,以便PersonController
访问私人成员Person
.但是,据我所知,friend
没有引入关键字来解决这些问题,在这里使用它将是一个不好的做法.
friend
在这里吗?给出以下向量:
5 4 1 2 3 1 4 5 3 2 3 2 1 5 4
_________ _________ _________
Run Code Online (Sandbox Code Playgroud)
我想对向量的每个5个元素应用sort.所以输出将是:
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
_________ _________ _________
Run Code Online (Sandbox Code Playgroud)
如何在MATLAB中实现无循环?
PS我还想提取排序索引以将它们应用于另一个向量.
为什么代码1在if语句之后没有括号而代码2不起作用.代码是通知重复相同的字符串.
代码1:
int main()
{
string previous = " ";
string current;
while(cin >> current){
if(previous == current)
cout << "repeated word: " << current << endl;
previous = current;
}
}
Run Code Online (Sandbox Code Playgroud)
代码2:
int main()
{
string previous = " ";
string current;
while(cin >> current){
if(previous == current)
{
cout << "repeated word: " << current << endl;
previous = current;
}
}
}
Run Code Online (Sandbox Code Playgroud)