猿问

是否有类似于 pythons 切片表示法的 c++ 函数?

在 python 中,您可以list[2:]获取第二个元素之后的每个元素。有没有办法用 c++ 中的数组做同样的事情?



繁星coding
浏览 191回答 2
2回答

弑天下

我希望我能找到一个可以接受的答案。不幸的是,在 C++ 中,你不能operator[]用多个参数重载,所以我operator()改用了。#include <iostream>#include <vector>template <typename T>class WrapperVector{private:&nbsp; &nbsp; std::vector<T> data;public:&nbsp; &nbsp; WrapperVector(size_t reserve_size)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; data.reserve(reserve_size);&nbsp; &nbsp; }&nbsp; &nbsp; WrapperVector(typename std::vector<T>::iterator start, typename std::vector<T>::iterator end)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; data = std::vector<T>(start, end);&nbsp; &nbsp; }&nbsp; &nbsp; // appends element to the end of container, just like in Python&nbsp; &nbsp; void append(T element)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; data.push_back(element);&nbsp; &nbsp; }&nbsp; &nbsp; /* instead of operator[], operator() must be used&nbsp; &nbsp; &nbsp; &nbsp; because operator[] can't accept more than one argument */&nbsp; &nbsp; // instead of self[x:y], use this(x, y)&nbsp; &nbsp; WrapperVector<T> operator()(size_t start, size_t end)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return WrapperVector<T>(data.begin() + start, data.begin() + end);&nbsp; &nbsp; }&nbsp; &nbsp; // instead of self[x:], use this(x)&nbsp; &nbsp; WrapperVector<T> operator()(size_t start)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return WrapperVector<T>(data.begin() + start, data.end());&nbsp; &nbsp; }&nbsp; &nbsp; // prints all elements to cout&nbsp; &nbsp; void print()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!data.size())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::cout << "No elements.\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; std::cout << data[0];&nbsp; &nbsp; &nbsp; &nbsp; size_t length = data.size();&nbsp; &nbsp; &nbsp; &nbsp; for(size_t i=1; i < length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::cout << ' ' << data[i];&nbsp; &nbsp; &nbsp; &nbsp; std::cout << '\n';&nbsp; &nbsp; }};int main(){&nbsp; &nbsp; WrapperVector<int> w(5);&nbsp; &nbsp; w.append(1);&nbsp; &nbsp; w.append(2);&nbsp; &nbsp; w.append(3);&nbsp; &nbsp; w.append(4);&nbsp; &nbsp; w.append(5);&nbsp; &nbsp; w(0).print();&nbsp; &nbsp; w(1, 3).print();&nbsp; &nbsp; // you can also save the slice&nbsp; &nbsp; WrapperVector<int> w2 = w(2);&nbsp; &nbsp; WrapperVector<int> w3 = w(2, 4);&nbsp; &nbsp; w2.print();&nbsp; &nbsp; w3.print();&nbsp; &nbsp; return 0;}现在您甚至可以重载它以接受三个参数来解释step,就像在 Python 中一样。我把它作为一个练习给你。

慕运维8079593

您可以使用迭代器的概念,而不是创建一个引入一些维护和某些限制的自定义类。两个迭代器用于表示一系列值。例如函数template<typename&nbsp;TIterator> foo(TIterator&nbsp;start,&nbsp;TIterator&nbsp;end);将采用仅由两个迭代器指定的对象范围。它是一个模板,因此它可以从不同的容器中获取迭代器。要从以这种方式给定的范围中选择子范围,您可以使用std::next()和std::prev()。例如对于一个std::vector<int>&nbsp;list;您可以使用从第三个元素开始的子范围调用 foo :foo(std::next(list.begin(),&nbsp;2),&nbsp;list.end());或使用从第三个到倒数第二个元素的子范围调用 foo:foo(std::next(list.begin(),&nbsp;2),&nbsp;std::prev(list.end(),&nbsp;1));如果您需要/想要复制子范围,您可以轻松地做到这一点。例如std::vector<int>&nbsp;subList(std::next(list.begin(),&nbsp;2), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::prev(list.end(),&nbsp;1));将创建一个向量sublist,其中包含来自 的倒数第三个到倒数第二个元素list。当然,这些只是简单的例子。在现实世界的应用程序中,您需要检查这些子范围是否有效/存在。优点是:没有额外的包装类无需复制任何数据(仅迭代器本身)标准库几乎只使用迭代器来表示范围,所以最好坚持这个概念。使用模板,您可以轻松地支持标准库中的所有容器,只要它们的迭代器满足参考中列出的要求。(您可以将上述示例与以及大多数其他容器一起使用std::array,std::list无需任何修改,list原因类型除外)迭代器是用户或第三方容器的接口,只要它们提供满足参考中列出的要求的迭代器。另一方面:代码变得有点复杂理解迭代器可能需要一些时间,因为它与其他语言使用的概念完全不同。
随时随地看视频慕课网APP

相关分类

Python
我要回答