0

How can I write python b[1:] in C++, which is an integer array index from 1 to n. Is it possible to represent it using vector?

def select_sort_rec(a):
     if len(a) <= 1:
        return a
     else:
        b = list(a)
        min_index = b.index(min(b))
        aux = b[min_index]
        b[min_index] = b[0]
        b[0] = aux
        return [aux] + select_sort_rec(b[1:])
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
  • 1
    There is no direct way to slice an array like you can do in python, but there are several other ways to accomplish it. It depends on what type you use to represent the array. Show us the C++ code where you want to do this and we can help you. – Max Langhof Aug 22 '19 at 11:00
  • It would help me a lot if you just show one using Vector STL perhaps. – Tech Me Aug 22 '19 at 11:01
  • If you remember that such "arrays" in Python are really called *lists*, then you could easily find support for that in [`std::list`](https://en.cppreference.com/w/cpp/container/list). – Some programmer dude Aug 22 '19 at 11:02
  • There you go, the duplicate even does exactly the same slicing! – Max Langhof Aug 22 '19 at 11:02
  • @MaxLanghof what would be the equivalent of python b[1: n-1] using C++ vector? – Tech Me Aug 22 '19 at 11:41
  • @TechMe Try to understand what is happening in the linked answer. Then you should be able to answer that question yourself. But you can also find the answer by searching for it. – Max Langhof Aug 22 '19 at 11:52

0 Answers0