Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:37:40+00:00 2026-05-31T09:37:40+00:00

Suppose a vector with values [0,1,2,3,4,5,6,7,8,9]. How can I create a vector that refers

  • 0

Suppose a vector with values [0,1,2,3,4,5,6,7,8,9]. How can I create a vector that refers to not necessarily contiguous values, e.g. [3,4,7,9], i.e. given by some index, by using STL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:37:41+00:00Added an answer on May 31, 2026 at 9:37 am

    You can express this as a transformation, e.g.:

    #include <valarray>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    
    template <typename T>
    void pick(std::vector<T>& result, const std::vector<T>& in, const std::vector<typename std::vector<T>::size_type>& s) {
      result.reserve(s.size());
      std::transform(s.begin(), s.end(), std::back_inserter(result),
                     [&in](typename std::vector<T>::size_type idx) {
                       return in.at(idx);
                     });
    }
    
    int main() {
      const std::vector<int> arr={0,1,2,3,4,5,6,7,8,9,10};
      std::vector<int> result;
      pick(result, arr, {3,4,7,9});
    }
    

    I used a lambda, but you can also use std::bind or the (now deprecated) std::bind2nd for this.

    The example with C++11’s std::bind makes pick:

    template <typename T>
    void pick(std::vector<T>& result, const std::vector<T>& in, const std::vector<typename std::vector<T>::size_type>& s) {
      result.reserve(s.size());
      std::transform(s.begin(), s.end(), std::back_inserter(result),
                     std::bind(static_cast<const T& (std::vector<T>::*)(typename std::vector<T>::size_type) const>(&std::vector<T>::at),  in, std::placeholders::_1));
    }
    

    It’s seriously ugly though because of the need to cast the member function pointer to resolve the overload of at (const vs non-const versions).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a named vector, bar : bar=c() bar[1997-10-14]=1 bar[2001-10-14]=2 bar[2007-10-14]=1 How can
Suppose that I have a vector of key-value pairs that I want to put
Suppose, I have a vector with following values in it id = 100 latitude
Suppose I have a vector of ints, std::vector<int> numbers; that is populated with a
This is not a question about putting std::auto_ptr into std::vector. Is there some vector
Hi suppose I want to create a map of the form map<int, vector<node> >
Suppose I have a vector<int> myvec and I want to loop through all of
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose, in MATLAB, that I have a matrix, A, whose elements are either 0
I am designing a REST API where some resources can be filtered through query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.