小编ham*_*els的帖子

Cpp核心指南在这个例子中浪费了什么?

Cpp核心指南中的例子浪费了什么?

P.9:不要浪费时间或空间

[...]

void lower(zstring s)
{
    for (int i = 0; i < strlen(s); ++i) s[i] = tolower(s[i]);
}
Run Code Online (Sandbox Code Playgroud)

是的,这是生产代码中的一个例子.我们留给读者来弄清楚浪费了什么.

来自https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rp-waste

c++ cpp-core-guidelines

11
推荐指数
4
解决办法
2022
查看次数

Scipy Sparse Cumsum

假设我有一个scipy.sparse.csr_matrix代表下面的值

[[0 0 1 2 0 3 0 4]
 [1 0 0 2 0 3 4 0]]
Run Code Online (Sandbox Code Playgroud)

我想计算就地非零值的累积和,这会将数组更改为:

[[0 0 1 3 0 6 0 10]
 [1 0 0 3 0 6 10 0]]
Run Code Online (Sandbox Code Playgroud)

实际值不是1,2,3 ......

每行中的非零值的数量不太可能相同.

怎么这么快?

目前的计划:

import scipy.sparse
import numpy as np

# sparse data
a = scipy.sparse.csr_matrix(
    [[0,0,1,2,0,3,0,4],
     [1,0,0,2,0,3,4,0]], 
    dtype=int)

# method
indptr = a.indptr
data = a.data
for i in range(a.shape[0]):
    st = indptr[i]
    en = indptr[i + 1]
    np.cumsum(data[st:en], out=data[st:en])

# print …
Run Code Online (Sandbox Code Playgroud)

python numpy sum scipy cumsum

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

如果在编译时已知/未知变量的值,则进行专门化

如何在编译时(实际编译和运行程序之前)知道/未知其参数之一的值的情况下专门化模板函数?

我怎么也弄不清楚.

想法1:

#include <type_traits>
#include <iostream>
int main(void){
    int a; //value of a is not known at compile time
    bool b = (a == a); //value of b is known at compile time.
    std::is_assignable< constexpr bool, bool >::value
}
//g++ magic.cpp -std=c++14
//error: wrong number of template arguments (1, should be 2)
// std::is_assignable< constexpr bool, bool >::value
Run Code Online (Sandbox Code Playgroud)

想法2:

#include <type_traits>
#include <iostream>
int main(void){
    const int a=1;
    int b = (a == a);
    std::cout <<  __builtin_constant_p (a)  << …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming

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

为程序选项提升多个值

当我a.out -i file0 file1在命令行输入时,我希望选项-i同时接收file0file1但是,-i只接收file0但不接收file1

但是,我发现我必须输入a.out -i file0 -i file1-i选择接收file0file1

可以boost::program_options这样吗?

代码改编自http://www.boost.org/doc/libs/1_62_0/libs/program_options/example/options_description.cpp

#include <boost/program_options.hpp>

using namespace boost;
namespace po = boost::program_options;

#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

// A helper function to simplify the main part.
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
    return os;
}

int main(int …
Run Code Online (Sandbox Code Playgroud)

c++ boost command-line-interface boost-program-options

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

有条件地调用其他构造函数

我想根据运行时条件调用同一类的不同构造函数。构造函数使用了不同的初始化列表(在之后的一堆东西:),因此我无法在构造函数内处理条件。

例如:

#include <vector>
int main() {
    bool condition = true;
    if (condition) {
        // The object in the actual code is not a std::vector.
        std::vector<int> s(100, 1);
    } else {
        std::vector<int> s(10);
    }
    // Error: s was not declared in this scope
    s[0] = 1;
}
Run Code Online (Sandbox Code Playgroud)

我想我可以使用指针。

#include <vector>
int main() {
    bool condition = true;
    std::vector<int>* ptr_s;
    if (condition) {
        // The object in the actual code is not a std::vector.
        ptr_s = new std::vector<int>(100, 1); …
Run Code Online (Sandbox Code Playgroud)

c++ constructor

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

OpenMp任务:不能通过引用传递参数

g++ -fopenmp main.cpp抱怨未定义的引用std::vector.如何解决这个问题?

libomp-dev在Ubuntu上安装了这个包.

main.cpp中

#include<vector>
#include<iostream>

template<typename T, typename A>
T recursiveSumBody(std::vector<T, A> &vec) {
    T sum = 0;
    #pragma omp task shared(sum)
    {
        sum = recursiveSumBody(vec);
    }
    return vec[0];
}

int main() {
    std::vector<int> a;
    recursiveSumBody(a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

未定义的参考文献

/tmp/ccTDECNm.o: In function `int recursiveSumBody<int, std::allocator<int> >(std::vector<int, std::allocator<int> >&) [clone ._omp_cpyfn.1]':
main.cpp:(.text+0x148): undefined reference to `std::vector<int, std::allocator<int> >::vector(std::vector<int, std::allocator<int> > const&)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

c++ openmp undefined-reference

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

指针/引用的CLION格式

从重新格式化,我想:

int* n;
int& m;
int const* o;
Run Code Online (Sandbox Code Playgroud)

相反,我得到:

int *n;
int &m;
int const *o;
Run Code Online (Sandbox Code Playgroud)

如何让CLion做我想做的事?

c++ formatting clion

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

无法将左值绑定到A <Cv2> &&

我认为通用引用(T&&)应该采用任何类型的引用.但以下不起作用.

当我尝试在我正在编写的库中进行const-correct时,我遇到了这个问题.我是C++的新手,之前没见过这样的东西.

TEST.CPP:

enum Cv_qualifier {
    constant,
    non_const
};
template <Cv_qualifier Cv> class A;
template<>
class A<Cv_qualifier::constant> {
public:
    template<Cv_qualifier Cv2> 
    void t(const A<Cv2>&& out) {}
};

template <>
class A<Cv_qualifier::non_const> {
public:
    template<Cv_qualifier Cv2> 
    void t(const A<Cv2>&& out) {}
};

int main()
{
    A<Cv_qualifier::non_const> a;
    A<Cv_qualifier::constant> b;
    a.t(b);
}
Run Code Online (Sandbox Code Playgroud)

错误(编译g++ test.cpp -std=c++11):

test.cpp: In function ‘int main()’:
test.cpp:24:10: error: cannot bind ‘A<(Cv_qualifier)0u>’ lvalue to ‘const A<(Cv_qualifier)0u>&&’
     a.t(b);
          ^
test.cpp:17:10: note:   initializing argument 1 of ‘void …
Run Code Online (Sandbox Code Playgroud)

c++ const universal-reference

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

用零并行填充std :: vector

我想std::vector<int>用openmp 填充零。如何快速做到这一点?

我听说循环遍历向量将每个元素设置为零很慢,而且std::fill快得多。现在还是这样吗?

将std :: vector <int>的每个值重置为0的最快方法

我是否必须手动将std::vector<int>区域划分为多个区域,#pragma omp for在每个线程上使用循环,然后std::fill在循环中使用?

c++ parallel-processing vector openmp

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

Numpy融合倍增并添加以避免浪费记忆

是否有可能将两个ndarray A和B相乘并将结果添加到C,而不会为A次B创建一个大的中间数组?

对于C = A乘B的情况,Numpy具有out关键字参数:

numpy.multiply(A, B, out=C)
Run Code Online (Sandbox Code Playgroud)

C + = A次B的情况怎么样?

python memory numpy multiplying

5
推荐指数
2
解决办法
1361
查看次数