What is the equivalent of Python's list[:x] in C++? -


in python, if have list l , want first x elements of it, call l[:x].

in c++, use vectors instead, don't know of easy way invoke first x elements of vector.

there several ways:

1) create vector v consisting of first x elements as:

 std::vector<t>  v { begin(l), begin(l) + x }; 

2) pass first x elements function, pair of iterators:

 f(begin(l), begin(l) + x); 

where f accepts 2 iterators arguments — explore standard algorithms <algorithm>, of them work on pair of iterators.

depending on use case, use of them.


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -