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

  • Home
  • SEARCH
  • 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 6067725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:38:22+00:00 2026-05-23T09:38:22+00:00

Given two vectors foo and bar , I want to output a vector of

  • 0

Given two vectors foo and bar, I want to output a vector of length foo.size() containing the index to the “closest” element of bar. I don’t like reinventing the wheel – are there any STL algorithms or otherwise to do this concisely?

#include <vector>
#include <cmath>
#include <float.h>

int main() {
    vector<double> foo;
    vector<double> bar;

    // example data setup
    double array_foo[] = {0.0, 1.0, 2.0, 3.0, 4.0,
                          5.0, 6.0, 7.0, 8.0, 9.0};
    double array_bar[] = {4.8, 1.5, 12.0};
    foo.assign(array_foo, array_foo + 10);
    bar.assign(array_bar, array_bar + 3);

    // output array
    vector<int> indices;
    indices.resize(foo.size());

    for(int i = 0; i < foo.size(); i++) {
        double dist = DBL_MAX;
        int idx = 0;
        // find index of closest element in foo
        for(int j = 0; j < bar.size(); j++) {
            if(abs(foo[i] - bar[j]) < dist) {
                dist = abs(foo[i] - bar[j]);
                idx = j;
            }
        }
        indices[i] = idx;
    }
    // expected result: indices = [1,1,1,1,0,0,0,0,0,2]
    return 0;
}
  • 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-23T09:38:22+00:00Added an answer on May 23, 2026 at 9:38 am

    This exact algorithm doesn’t exist, but you could implement it in an idiomatic STL way by using std::min_element and a custom functor:

    template <typename T>
    T norm(const T& a, const T& b)
    {
        return abs(b - a);
    }
    
    template <typename T>
    struct closer_compare
    {
        closer_compare(const T& to) : to(to) {}
        bool operator()(const T& a, const T& b) const
        {
            return norm(a, to) < norm(b, to);
        }
        const T& to;
    };
    
    template <typename It1, typename It2, typename OutIt>
    void find_nearest_indices(It1 in1_begin, It1 in1_end, It2 in2_begin, It2 in2_end, OutIt out)
    {
        typedef typename std::iterator_traits<It1>::value_type value;
        for (It1 it = in1_begin; it != in1_end; ++it)
        {
            It2 closest = std::min_element(in2_begin, in2_end, closer_compare<value>(*it));
            *out++ = std::distance(in2_begin, closest);
        }
    }
    

    Your algorithm would then be replaced with:

    find_nearest_indices(foo.begin(), foo.end(), bar.begin(), bar.end(), indices.begin());
    

    I tested with your input and got your expected results.

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

Sidebar

Related Questions

Given two vectors of integers, how to determinate if there's some element from 1st
I'm trying to use std::transform to merge two equal size vectors into a vector
Given two 3D vectors A and B, I need to derive a rotation matrix
Given two vectors X and Y , I have to find their correlation, i.e.
The dot product of two n-dimensional vectors u=[u1,u2,...un] and v=[v1,v2,...,vn] is is given by
Currently I have two larger vectors of 50+ strings I want to be able
I have two vectors in a game. One vector is the player, one vector
Given two vectors in three dimensions pointing different directions. I would like the first
Given two orthogonal unit vectors, A and B, and two different orthogonal unit vectors
I have a number of vector containers of varying size each containing doubles. I

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.