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 6566527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:13:39+00:00 2026-05-25T14:13:39+00:00

I know it is a very bad idea, so other suggestions on how to

  • 0

I know it is a very bad idea, so other suggestions on how to do it efficiently will be well-received.

Here’s the thing. I have map<string,vector<string> > , I want to search for a key and return its corresponding value (vector of strings in this case). Reason I insist on returning (rather than just iterating) is I need to search the values returned in some other vector.

An example will make this clear:

Input:

key1 ---> {2,3,4}
key2 ---> {1}
key3 ---> {2,12,11,9}

For key1 as input, vector with values 2,3,4 should be returned. Now these 2,3,4 values need to be searched in other vector of strings. What is the most efficient way to do this?

I tried something like this:

vector<string> returnEdges(string key)
{
    for (map<string, vector<string> >::iterator it=outgoing.begin();
    it!=outgoing.end();++it)
    {
        if (key.compare((*it).first)==0)
        {
            return (*it).second;
        }
    }


    //return string<;//what should I return here????


}

1) How should I return empty vector in case key is not found?

2) what is the best way to implement this?

I hope the question is clear.

EDIT: As I wrote the question, I thought why not return an iterator? Do the people at SO give their approval for this idea?

  • 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-25T14:13:40+00:00Added an answer on May 25, 2026 at 2:13 pm

    1) Returning an iterator is a fine idea. When you do this, the natural way to indicate the “not found” case is to return the .end() iterator. This has the down-side that the abstraction is somewhat leaky: the caller has to be able to get at this .end() value in order to compare to it for error checking, and the returned iterator exposes a richer interface than you’d like (the client code shouldn’t really play around with incrementing and decrementing the iterator).

    2) Returning an empty vector is as simple as creating an empty vector and returning it. Creating an empty vector = constructing a vector which is empty. This is what you get from – drum roll – the default constructor of the vector class.

    3) You don’t need to, and shouldn’t, implement the search loop yourself. The standard library already implements this for you. (There is a specialized find function for map s because of the key/value distinction. For sequences like list, vector and deque, prefer the free function std::find, which comes from <algorithm>.

    4) You should prefer to accept function parameters (when they are instances of classes, like std::string) and return data (especially complex things like a vector of strings) by const reference. Passing and returning by value implies a copy; sometimes the compiler can optimize this away, but it’s not as reliable as we’d like. Besides, the reason you’re using C++ in the first place is to have that level of control over things, right? If not, then don’t torture yourself with it.

    However, you can’t do that if you’re going to return a newly-created value some of the time. Yet another way to design the interface is to return a pointer to the vector of strings (note that pointer arithmetic on these will be invalid) within the map, or a NULL pointer if the value is not found. This avoids copying and distinguishes a “not found” result from an actual empty vector within the data, but it means the client code has to deal with an icky raw pointer.

    5) Having ‘return’ in the name of a function is useless, as returning is what functions do. OTOH, it is a good idea to name things in a way that makes it evident why the parameters are what they are.

    6) With iterators for complex types, it is often a good idea to set up typedefs.

    Returning an iterator is as simple as:

    typedef map<string, vector<string> >::iterator graph_iterator;
    graph_iterator edges_named(const string& node_name) {
        return outgoing.find(node_name);
    }
    

    Returning a vector of strings is as simple as:

    typedef map<string, vector<string> >::iterator graph_iterator;
    vector<string> edges_named(const string& node_name) {
        graph_iterator it = outgoing.find(node_name);
        return it == outgoing.end() ? vector<string>() : it->second;
    }
    

    Returning a pointer is as simple as:

    typedef map<string, vector<string> >::iterator graph_iterator;
    vector<string>* edges_named(const string& node_name) {
        graph_iterator it = outgoing.find(node_name);
        return it == outgoing.end() ? NULL : &(it->second);
    }
    

    Choose wisely.

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

Sidebar

Related Questions

I know very little about encryption/hashing. I have to hash an encryption key. The
I am new to javascript so sorry I don't know very much. I have
The code here is X++. I know very little about it, though I am
I don't know MySQL very well so I need a bit of help with
I know this works very well: def locations(city, *other_cities): print(city, other_cities) Now I need
i know that it's a bad idea to fetch the current_user in a model.
So I want to know if this is a good idea or a bad
I know it's a bad idea but I need to do this. I'm about
yes i know, making bitwise ops on double values seems like a bad idea,
I know very little about Flash, and so is not programming in it. 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.