小编Vin*_*ent的帖子

何时使用迭代器的`value_type`?

我试图了解何时iterator::value_type实际使用.

因为迭代器的所有运算符似乎只使用iterator::pointeriterator::reference.

问:是否iterator::value_type实际使用的东西?

额外的问题:迭代器是否继承自

std::iterator<std::random_access_iterator_tag, int, std::ptrdiff_t, bool*, bool&>
Run Code Online (Sandbox Code Playgroud)

提出一些语义问题?

编辑:要理解为什么我问这个问题,这是因为我正在为一个类型的迭代器工作,pointer并且reference是代理类.

c++ iterator c++11 semantics

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

明确默认的构造函数有什么作用?

考虑以下:

template <class T>
struct myclass
{
    using value_type = T;
    constexpr myclass() = default;
    constexpr myclass(const myclass& other) = default;
    constexpr myclass(const myclass&& other) = default;
    T value;
};
Run Code Online (Sandbox Code Playgroud)
  • 这些函数的构造函数体是等价的?
  • 是否myclass<int> x;初始化的整数0
  • 有关myclass<std::vector<int>> x;默认移动构造函数的作用是什么?它是否称为向量的移动构造函数?

c++ constructor default default-constructor c++11

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

boost.asio 和目前的网络 TS 最大的区别是什么?

C++ 委员会目前正在制定网络技术规范。我想知道与boost::asio. 而且,我好几次听说in中的udp实现boost::asio效率不高,我想知道TS是否试图解决这个问题。

c++ networking boost boost-asio c++11

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

未对齐的内存访问:是否定义了行为?

请考虑以下代码:

#include <iostream>

int main()
{
    char* c = new char('a');
    char ac[4] = {'a', 'b', 'c', 'd'};
    unsigned long long int* u = reinterpret_cast<unsigned long long int*>(c);
    unsigned long long int* uc = reinterpret_cast<unsigned long long int*>(&ac[3]);
    *u = 42;
    *uc = 42;
    std::cout<<*u<<" "<<*uc<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这被认为是有效的代码,还是内存泄漏/未定义的行为?我问,因为通过:

*u = 42;
*uc = 42;
Run Code Online (Sandbox Code Playgroud)

我们正在访问程序无法访问的字节(我猜).

c++ memory-leaks memory-access memory-alignment c++11

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

非空原始指针和 nullptr 之间的小于运算符

非空原始指针的操作nullptr < ptrptr < nullptr定义是否良好ptr != nullptr?欢迎引用 C++ 标准。

c++ standards comparison-operators nullptr c++11

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

混合void_t和可变参数模板?

请考虑以下代码:

template <class F, class... Args, class = std::void_t<>>
struct is_invokable
: std::false_type {};
template <class F, class... Args>
struct is_invokable<F, Args..., std::void_t<std::invoke_result_t<F, Args...>>>
: std::true_type {};
Run Code Online (Sandbox Code Playgroud)

目标是有一个特性,它能够判断类型F的可调参数是否可以使用类型的参数调用Args....

但是,它无法编译,因为:

error: parameter pack 'Args' must be at the end of the template parameter list
Run Code Online (Sandbox Code Playgroud)

在C++ 17中执行此操作的(优雅)方式是什么?

c++ sfinae variadic-templates c++17 void-t

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

结合void_t和enable_if?

在 中C++17void_t允许使用class/struct模板轻松执行 SFINAE:

template <class T, class = void>
struct test {
    static constexpr auto text = "general case";
};

template <class T>
struct test<T, std::void_t<decltype(std::begin(std::declval<T>())>> {
    static constexpr auto text = "has begin iterator";
};
Run Code Online (Sandbox Code Playgroud)

里面的东西void_t是一个类型。void_t我的问题是:当里面的内容是类型特征时,如何做同样的事情。使用enable_if效果很好:

template <class T>
struct test<T, std::void_t<std::enable_if_t<std::is_class_v<T>>> {
    static constexpr auto text = "is a class";
};
Run Code Online (Sandbox Code Playgroud)

是否有更短/更优雅的方式来写这个,或者“正确的方式”来做到这一点,真的是结合void_tand enable_if

sfinae enable-if template-meta-programming c++17 void-t

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

默认参数和空列表初始化

考虑以下代码,一个简单的类,其构造函数采用带有默认值的参数.

// Version 1
template <class T>
struct object1 {
    using type = T;
    constexpr object1(const type& val = type()): value(val) {}
    type value;
};

// Version 2
template <class T>
struct object2 {
    using type = T;
    constexpr object2(const type& val = {}): value(val) {}
    type value;
};

// Main
int main(int argc, char* argv[]) {
    using type = /* Something */;
    object1<type> x1;
    object2<type> x2;
    auto value1 = x1.value;
    auto value2 = x2.value;
    // Is there certain types …
Run Code Online (Sandbox Code Playgroud)

c++ default-constructor default-arguments c++11 list-initialization

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

Python matplotlib:plot3D,颜色为4D

我试图从x,y,z点列表制作3D图,我想根据第四个变量rho的值绘制颜色.

目前我有;

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot3D(cell_x, cell_y, cell_z, linestyle='None', marker='o', markersize = 5, antialiased=True)
ax.set_xlim3d(0.45, 0.55)
ax.set_ylim3d(0.45, 0.55)
ax.set_zlim3d(0.45, 0.55)
Run Code Online (Sandbox Code Playgroud)

如何添加cell_rho(我的第四个数组)作为我的x,y,z点的颜色?(例如喷射色图).

非常感谢你.

编辑:我不能使用散点图因为我的18000点散点图与仅带标记的plot3d相比非常慢.

python matplotlib color-mapping mplot3d

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

条件的 Python 子列表

我有 3 个列表x, yz我用以下方法绘制它们:

ax.plot3D(x, y, z, linestyle = 'None', marker = 'o').
Run Code Online (Sandbox Code Playgroud)

只绘制点 where 的最简单方法是什么x > 0.5

(我的问题是如何在条件下定义子列表而不在该列表上进行 for 循环)。

python list

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