任何人都可以高度直觉地知道何时使用它们?
参考文献:
我正在关注这个Heroku教程:https://devcenter.heroku.com/articles/getting-started-with-python-o当我尝试在virtualenv中安装gunicorn时,我收到此错误:
(venv)jabuntu14@ubuntu:~/Desktop/helloflask$ pip install gunicorn
Downloading/unpacking gunicorn
Downloading gunicorn-19.1.1-py2.py3-none-any.whl (104kB): 104kB downloaded
Installing collected packages: gunicorn
Compiling /home/jabuntu14/Desktop/helloflask/venv/build/gunicorn/gunicorn/workers /_gaiohttp.py ...
File "/home/jabuntu14/Desktop/helloflask/venv/build/gunicorn/gunicorn/workers /_gaiohttp.py", line 64
yield from self.wsgi.close()
^
SyntaxError: invalid syntax
Successfully installed gunicorn
Cleaning up...
Run Code Online (Sandbox Code Playgroud)
但是,当我运行$ foreman start时,它似乎正常工作.
这个错误有多重要?知道怎么解决吗?
我在BitBucket中有一套私人Git回购.我想通过SSH克隆它们,以便克隆可以自动化而无需输入密码.但是,我想推送HTTPS,因为我想推送一个不同的用户名.
PC是一个普通的PC,我想区分是谁推动了改变,但我不关心谁克隆它们.
有没有办法做到这一点?谢谢!
我有一个代码,其中在函数的末尾我需要从 int 转换为数组的所有元素的两倍,以便能够在退出函数之前执行最终 push_back。我现在拥有的代码是:
template <class T, size_t dims> class A {
typedef typename std::array<int, dims> ArrayInt;
typedef typename std::array<double, dims> ArrayDouble;
typedef typename std::vector <ArrayDouble> VectorDouble;
/* ...*/
foo() {
/* ...*/
ArrayInt myArrayInt;
ArrayDouble myArrayDouble;
VectorDouble myVectorDouble;
/* Initialize myArrayInt
Do some other stuff */
for (int i = 0; i < dims; ++i)
myArrayDouble[i] = static_cast<double>(myArrayInt[i]);
myVectorDouble.push_back(myArrayDouble);
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我对这些行感到不舒服:
for (int i = 0; i < dims; ++i)
myArrayDouble[i] = static_cast<double>(myArrayInt[i]);
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
谢谢你。
我有一个 Docker 容器在 AWS EC2 实例中运行,我通过 ssh 进入 EC2 实例,运行我的 docker 容器,并在 docker 容器中运行一个screen分离会话的脚本:
screen -dmS test my_script.sh
Run Code Online (Sandbox Code Playgroud)
在同一个终端内,我没有问题screen -x test。
但是,如果我现在打开一个新终端,通过 SSH 连接到 EC2 实例,进入 docker 容器 ( sudo docker exec -it container_id /bin/bash) 并运行,screen -x test我将收到错误消息:
Must be connected to a terminal.
Run Code Online (Sandbox Code Playgroud)
显示输出screen -ls:
There is a screen on:
2122.test (11/18/2016 09:45:50 AM) (Detached)
1 Socket in /var/run/screen/S-root.
Run Code Online (Sandbox Code Playgroud)
因此我无法从其他任何地方访问屏幕。任何帮助在这里表示赞赏。谢谢!
注意:我在 Google 和 SO 上看到了许多类似的问题,但是我能理解/尝试的所有回复都不适用于我的设置。
似乎我无法完全理解移动语义:我想std::vector从外部函数填充(类的成员).目前,我有类似的东西:
void fillVector(MyClass & myclass) {
std::vector<int> vec;
/* Filling vec */
// ...
myclass.setVector(vec);
}
class MyClass {
public:
setVector(const std::vector<int> & v) { v_ = v;}
private:
std::vector<int> v_;
};
int main() {
MyClass myclass;
fillVector(myclass);
/* Use myclass.v_ somehow */.
}
Run Code Online (Sandbox Code Playgroud)
我有这个代码很长一段时间,它工作正常.现在,我无法理解它是如何工作的,因为我将对一个将被销毁的向量的引用作为参考.我的问题是:
fillVector和MyClass::setVector()?我认为可以用移动语义完成,但我无法弄清楚如何.谢谢!
我一直在使用相同的std::vector<int>,以便尽量避免分配deallocating.在几行中,我的代码如下:
std::vector<int> myVector;
myVector.reserve(4);
for (int i = 0; i < 100; ++i) {
fillVector(myVector);
//use of myVector
//....
myVector.resize(0);
}
Run Code Online (Sandbox Code Playgroud)
在每次迭代中,myVector最多将填充4个元素.为了制作高效的代码,我想永远使用myVector.但是,在被摧毁myVector.resize()的元素中myVector.我明白这myVector.clear()会产生同样的效果.
我想如果我可以覆盖现有的元素,myVector我可以节省一些时间.但是我认为std::vector不能做到这一点.
有没有办法做到这一点?创建一个覆盖元素的本土实现是否有意义?
我试图理解移动语义,所以我做了以下测试:
#include <iostream>
#include <array>
using namespace std;
void tryToMove(array<double,3> && v) {
array<double,3> v_ = std::move(v);
std::cout << v_[0] << " " << v_[1] << " " << v_[2] <<'\n';
}
int main () {
array<double,3> v{1,2,3};
tryToMove(std::move(v));
std::cout << v[0] << " " << v[1] << " " << v[2] <<'\n';
}
Run Code Online (Sandbox Code Playgroud)
std::cout因为v_应该被移入,所以我期待主要的sementation错误tryToMove.但是,输出是:
1 2 3
1 2 3
Run Code Online (Sandbox Code Playgroud)
这里到底发生了什么?
谢谢!
我试图弄清楚std::vector<int>用常int量值增加a的所有元素的最佳方法是什么.
换句话说,如果我有一个带元素的向量: 1 2 3 4 5
我想做点什么
vect += 5;
Run Code Online (Sandbox Code Playgroud)
所以元素将是:6 7 8 9 10.
我试图超载,operator +=但事实证明我不知道该怎么做:SI试过这个:
std::vector<int> & operator += (const int & increment) {
for (int &i : *this)
*this[i] = *this[i] + increment;
}
Run Code Online (Sandbox Code Playgroud)
这个编译,但每当我使用它我得到这个错误:
no match for ‘operator+=’ (operand types are ‘std::vector<int>’ and ‘int’)
vec += 3;
^
Run Code Online (Sandbox Code Playgroud)
有什么建议?我想这样做而不是常规increment(vector, value)功能.
谢谢!
我想找到一个向量的最小值:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<double> v{2, 0, 4};
double minT = *std::min_element(v.begin(), v.end(),
[](double i1, double i2) {
std::cout << "Comparing: " << i1 << " " << i2 << " " << ((i1 < i2)? i1:i2) << " " << '\n';
return (i1 < i2)? i1:i2;
});
cout << "Minimum is: " << minT << '\n';
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码的输出是:
Comparing: 0 2 0
Comparing: 4 2 2
Minimum is: 4 …Run Code Online (Sandbox Code Playgroud) 我正在实现 Fast Marching 算法,它是某种连续 Dijkstra 算法。正如我在许多论文中读到的那样,斐波那契堆是最适合此目的的堆。
然而,当使用 callgrind 分析我的代码时,我发现以下函数占用了 58% 的执行时间:
int popMinIdx () {
const int idx = heap_.top()->getIndex();
heap_.pop();
return idx;
}
Run Code Online (Sandbox Code Playgroud)
具体来说,它pop()占用了整个执行时间的 57.67%。
heap_定义如下:
boost::heap::fibonacci_heap<const FMCell *, boost::heap::compare<compare_cells>> heap_;
Run Code Online (Sandbox Code Playgroud)
花费“那么多”时间是否正常,或者我可以做些什么来提高性能?
抱歉,如果没有提供足够的信息。我尝试尽可能简短。如果需要,我会添加更多信息。
谢谢你!
大多数情况下,while循环用于检查while循环体中发生的事件.并且主要用于给出"确定的"迭代次数.我们假设我们需要知道我们所处的迭代次数,一般来说:
一般While循环:
bool flag = false;
int it = 0;
while (!flag)
{
//... do something using it
++it;
flag = getFlag();
}
Run Code Online (Sandbox Code Playgroud)
一般For循环,迭代次数是循环的固有:
for(int it = 0; it < N; ++it)
{
//... do something using it
if (getFlag())
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,对于没有设置迭代次数的情况(也就是说,我们循环直到偶数发生)我从未见过如下情况:
for(int it = 0; !getFlag(); ++it)
{
//...do something using it
}
Run Code Online (Sandbox Code Playgroud)
但我总是看到像我写的第一个while循环.有什么真正的区别吗?在while循环中它被认为是更好的风格吗?因为对我来说,for循环更紧凑,易于阅读和遵循.