Brennan McGowan
Brennan McGowan

Reputation: 15

Is there anything similar to Python's indexing in c++?

I am working on using parts of an array of chars in C++ and was trying to figure out the easiest way to do this. I know in Python you can simply do something like str[1:] which will give you the entire array except the first position and was wondering if C++ has any analogs to this or if not, what would be the easiest way to implement this.

Upvotes: 1

Views: 248

Answers (1)

bobah
bobah

Reputation: 18864

For right now you can try using as header-only Boost.Range (sliced, etc.) library (+ there is a chapter about it at theboostcpplibraries.com).

Example from the library documentation:

#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <vector>

int main(int argc, const char* argv[])
{
    using namespace boost::adaptors;
    using namespace boost::assign;

    std::vector<int> input;
    input += 1,2,3,4,5,6,7,8,9;

    boost::copy(
        input | sliced(2, 5),
        std::ostream_iterator<int>(std::cout, ","));

    return 0;
}

In future there will probably be something called span<T> in the standard (discussion here).

Upvotes: 1

Related Questions