大多数时候我看到std::move这里发布,它引用了<utility>版本。
将std::move在<algorithm>实际上做什么它的名字所暗示的,移动的,而std::move在<utility>投射其参数的x值,这基本上是只为最终x值移动到左值的预处理步骤。那么,move当它们的功能不同时,将它们命名为两者是不是有点令人困惑?
我想编写一个带有一个参数的C++函数,以便可以传入以下任何一种类型:
std::vector<int>
std::array<int>
int array[numElements]
int *ptr = new int[numElements]
etc
Run Code Online (Sandbox Code Playgroud)
模板是实现这一目标的最佳方式吗?
int main(int argc, char const *argv[])
{
int x = 4;
int y = 2;
const int cell = x/y;
auto a = std::bitset<20>{cell}; //fails
auto b = std::bitset<20>(cell); //works
}
Run Code Online (Sandbox Code Playgroud)
为什么不允许std::bitset我在这里使用花括号构建,但可以使用括号构建?如果cell是 a constexpr,则两者都会编译。
编译错误:
test.cpp:21:29: error: non-constant-expression cannot be narrowed from type 'int' to 'unsigned long long' in initializer list [-Wc++11-narrowing]
auto a = std::bitset<20>{x*y}; //fails
^~~
test.cpp:21:29: note: insert an explicit cast to silence this issue
auto a = std::bitset<20>{x*y}; //fails
^~~
static_cast<unsigned …Run Code Online (Sandbox Code Playgroud) 当您将鼠标悬停在 VS Code 中的变量(例如函数名称)上时,会出现一个小弹出窗口,显示函数的定义位置以及其他一些快速详细信息。该功能的名称是什么?
我正在一台新计算机上设置 VS Code,我正在尝试获得该功能,但不确定它叫什么。
这与我之前提出的关于emplace_back在成对向量上使用的问题有些相关。将一对插入 std::vector 时,emplace_back() 与 push_back
现在我的问题与emplace_back在向量向量上使用有关。
这是我用评论质疑的代码
std::vector<std::vector<int>> matrix;
matrix.emplace_back({1,2,3}); //doesn't compile
matrix.emplace_back(1,2,3); //doesn't compile
matrix.push_back({1,2,3}); //works and does what is expected (insert a vector made of {1,2,3} into matrix);
matrix.emplace_back(std::vector<int>{1,2,3}); //works but
//defeats the purpose of using emplace_back since this makes a copy
//and is thus equivalent to push_back in this case?
matrix.emplace_back(3,2) //this compiles,
//but it seems to insert a vector of size 3 made of 2s into the matrix.
//not actually sure …Run Code Online (Sandbox Code Playgroud) 我需要创建一个 lambda 函数,它可以接受 aint *或 a float *,例如,类似
auto func = [](void* ptr) {
// do some assignments with the pointer
}
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用该void *方法,但我想知道还有哪些其他方法可以实现我想要的目标?
我无法访问代码库中的 C++17 功能,因此必须坚持使用 C++14 或更低的语义。
这是一个更普遍的问题:const如果通过值传递函数参数,是否有任何意义?
在我正在研究的代码中,我看到了很多以下内容:
void some_function(const std::vector<double> some_vec);
Run Code Online (Sandbox Code Playgroud)
std::vector是按值传递的,那么接下来的是什么const呢?
就像我理解函数是否通过引用传递向量一样:
void some_function(const std::vector<double> &some_vec);
Run Code Online (Sandbox Code Playgroud)
但我认为const前者没有任何意义.
std::vector<bool> 使用代理迭代器。
因此,以下代码将无法编译(代码取自相关问题中已接受的答案):
vector<bool> v = {true, false, false, true};
for (auto& x : v)
x = !x;
Run Code Online (Sandbox Code Playgroud)
在相关问题中,已接受的答案指出,要就地修改向量的分量,我们必须使用
for (auto&& x : v)
x = !x;
Run Code Online (Sandbox Code Playgroud)
但如果我只是这样做:
for (auto x : v)
x = !x;
Run Code Online (Sandbox Code Playgroud)
这会产生相同的结果。那么&&不需要吗?
进一步为什么以下2个代码不修改组件?
for (bool &&x : v)
x = !x;
Run Code Online (Sandbox Code Playgroud)
和
for (bool x : v)
x = !x;
Run Code Online (Sandbox Code Playgroud) 以下代码始终有效还是依赖于编译器/平台?显然,我可以edges使用值构造函数进行初始化,但是我很好奇operator=在edges初始化为大小 0时复制赋值是否在此处工作,然后设置为等于带括号的 r 值。
它适用于我的 macbook。
std::vector<std::vector<int>> edges;
edges = {{1,2,3},{4},{5,6}};
Run Code Online (Sandbox Code Playgroud) 我很确定我以前在这里看到过这个问题,但是当我尝试搜索时似乎找不到该帖子,而且我不记得答案了。
为什么没有emplace或emplace_back为std::string?
我认为这与 的使用无关char,因为您可以拥有一个字符向量,并且emplace_back在向量中输入一个字符没有问题。