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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:39:27+00:00 2026-05-24T23:39:27+00:00

I need help figuring out bits of code. I need a function that loads

  • 0

I need help figuring out bits of code. I need a function that loads up a vector with given strings and returns a pointer. I plan to use it to generate multiple vectors and then use the pointer to display them.

I am not sure how to set up both the return of the pointer by the function and the later display of the vectors. Please make suggestions only on lines that contain //HELP NEEDED HERE

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int pointerReturner (string str1, string str2) //HELP NEEDED HERE
{
    vector<string> vList;
    vList.push_back(str1); 
    vList.push_back(str2); 
    return vList; //HELP NEEDED HERE
}

int main(int argc, char* argv[]) {

    vector<string> vMakeList1;
    vMakeList1 =pointerReturner("Honda","Toyota");//HELP NEEDED HERE
    for (vector<string>::iterator n=vMakeList1.begin(); n!=vMakeList1.end();++n)
    {
        cout<<*n<<endl;
    }

    vector<string> vMakeList2;
    vMakeList2=pointerReturner("Chrysler","Ford");//HELP NEEDED HERE
    for (vector<string>::iterator n=vMakeList2.begin(); n!=vMakeList2.end();++n)
    {
        cout<<*n<<endl;
    }

    cin.get();
    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-24T23:39:28+00:00Added an answer on May 24, 2026 at 11:39 pm

    You have to allocate the vector dynamically using the new operator. Otherwise the vector is just an automatic variable that is destroyed after the function pointerReturner ends. You also have to change the return type of this function from int to a pointer to a vector (vector*).
    But if you allocate an object dynamically you have to destroy it explicitly using the delete operator, otherwise the allocated memory will never be released which would result in a memory leak.

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    vector<string>* pointerReturner (string str1, string str2)
    {
        vector<string> *vList = new vector<string>();
        vList->push_back(str1); 
        vList->push_back(str2); 
        return vList;
    }
    
    int main(int argc, char* argv[]) {
    
        vector<string> *vMakeList1;
        vMakeList1 = pointerReturner("Honda","Toyota");
        for (vector<string>::iterator n=vMakeList1->begin(); n!=vMakeList1->end();++n)
        {
            cout<<*n<<endl;
        }
        delete vMakeList1;
    
        vector<string> *vMakeList2;
        vMakeList2=pointerReturner("Chrysler","Ford");
        for (vector<string>::iterator n=vMakeList2->begin(); n!=vMakeList2->end();++n)
        {
            cout<<*n<<endl;
        }
        delete vMakeList2;
    
        cin.get();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help figuring out bits of code. I am not sure how to
I have a vector of strings. Need help figuring out how to convert it
Need help figuring out how to do this. My code: my %hash; $hash{'1'}= {'Make'
I need help figuring out how to convert data that comes in from a
i need help with disk_total_space function.. i have this on my code <?php $sql=select
I need help figuring out why my authentication ticket is expiring after about an
I need help figuring out some regular expressions. I'm running the dig command and
I need help figuring out how to change out the view in my application.
I need help figuring out these php print (echo) statements and where to place
Need help in figuring out a way to fetch a particular block of content

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.