小编vel*_*s14的帖子

为什么这个 C++ 工作?(变量的声明/定义)

为什么我可以在 for 循环的每次迭代中在 for 循环 [for (auto val: k0L){...}] 中声明和定义 3 个变量?当我执行 g++ code.cpp 时,编译器不会抱怨。我知道一个变量只能声明一次。我知道我不能写 int a = 5; int a = 6; 在 main() 范围内。但是,这就是我在 for 循环中所做的。谢谢!

#include <iostream>
#include <vector>
#include <fstream>
#include <math.h>
#include <algorithm>

#define PI 3.14159265

std::vector<double> linspace (double start, double end, size_t points) { // will be used in main(), details of this function are not important.
    std::vector<double> res(points);
    double step = (end - start) / (points - 1);
    size_t i = 0;
    for …
Run Code Online (Sandbox Code Playgroud)

c++ variables for-loop declaration stdvector

1
推荐指数
1
解决办法
82
查看次数

可能使用 NVidia 使 C++ 中的 for 循环更快的方法?

我想让 C++ 函数更快。我正在向您询问可能的方法。

我最多可以使用 32 个 OMP 线程。

我可以使用 NVidia GPU。

该函数的 MWE 为:

#include <iostream>
#include <complex>
#include <cmath>

typedef std::numeric_limits<double> dbl;
#define _USE_MATH_DEFINES

#include <omp.h>

const std::complex<double> I(0.0, 1.0); // imaginary unit, I*I = -1
std::complex<double> zero_imag (0.0, 0.0);

const int N_rs = 1500;
const int l_max = 70;
const int lmax = 70;
const int N_thetas = l_max + 1;
const int N_phis = 2 * l_max + 2;
const int N_ps = 600;
const int nphi …
Run Code Online (Sandbox Code Playgroud)

c++ parallel-processing optimization performance cuda

1
推荐指数
1
解决办法
287
查看次数

迭代一个非常非常长的 std::vector 问题

我有一个std::vector<std::tuple<int, int>>能够容纳很多潜在元素的东西。

我事先不知道它会有多少个元素。这取决于我如何运行代码(使用什么输入参数)。

我想迭代这个向量。

目前,我正在做:

for (long long int idx = 0; idx <= (vec.size() - 1); idx++) {
    do_something(vec[idx]);
}
Run Code Online (Sandbox Code Playgroud)

这会产生警告: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::tuple<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]

从SO中,我知道vec.size()返回 a std::vector<std::tuple<int, int> >::size_type,我知道它可能与以下内容不同size_t/sf/answers/1996006421/

我的问题是:我该如何编写 for 循环来迭代向量的所有元素,而不发出警告?

例如, anint是不够的,我确信向量将容纳多个65535元素。

另外,long unsigned int编译器告诉我.size()返回的内容可能不足以容纳我的向量的所有元素:来自https://en.cppreference.com/w/cpp/language/types,它似乎可以上升至4,294,967,295仅。

根据谷歌的说法,4 294 967 295 …

c++ for-loop stdvector

1
推荐指数
3
解决办法
145
查看次数