我试图了解何时iterator::value_type实际使用.
因为迭代器的所有运算符似乎只使用iterator::pointer和iterator::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是代理类.
考虑以下:
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;默认移动构造函数的作用是什么?它是否称为向量的移动构造函数?请考虑以下代码:
#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)
我们正在访问程序无法访问的字节(我猜).
非空原始指针的操作nullptr < ptr和ptr < nullptr定义是否良好ptr != nullptr?欢迎引用 C++ 标准。
请考虑以下代码:
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++17,void_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?
考虑以下代码,一个简单的类,其构造函数采用带有默认值的参数.
// 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
我试图从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相比非常慢.
我有 3 个列表x, y,z我用以下方法绘制它们:
ax.plot3D(x, y, z, linestyle = 'None', marker = 'o').
Run Code Online (Sandbox Code Playgroud)
只绘制点 where 的最简单方法是什么x > 0.5?
(我的问题是如何在条件下定义子列表而不在该列表上进行 for 循环)。
c++ ×7
c++11 ×6
c++17 ×2
python ×2
sfinae ×2
void-t ×2
boost ×1
boost-asio ×1
constructor ×1
default ×1
enable-if ×1
iterator ×1
list ×1
matplotlib ×1
memory-leaks ×1
mplot3d ×1
networking ×1
nullptr ×1
semantics ×1
standards ×1