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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:21:16+00:00 2026-06-07T02:21:16+00:00

For loop iterates through the vector and show all strings on the console successfully:

  • 0

For loop iterates through the vector and show all strings on the console successfully:

int main(int argc, const char * argv[])
{
    vector<string>stringVector;
    string newStringEntry;

    cin>>newStringEntry;
    stringVector.push_back(newStringEntry);

    cin>>newStringEntry;
    stringVector.push_back(newStringEntry);

    for (vector<string>::iterator i = stringVector.begin(); i != stringVector.end(); ++i) 
    {
        cout<<*i<<endl;
    }
}

But this makes the code hard to read (said our teacher) now we have to build a class for the for loop and the main should look like this after the change:

int main(int argc, const char * argv[])
{
    vector<string>stringVector;
    string newStringEntry;

    cin>>newStringEntry;
    stringVector.push_back(newStringEntry);

    cin>>newStringEntry;
    stringVector.push_back(newStringEntry);

    showStrings();
}

The problem I see is that I cannot just copy paste the for loop to an own class because
the vector name I declare in the main is not known in the class so I get an error.

How can I get this working?

My class so far looks like this:

class VectorHelp {
    void showStrings() const 
    {
        for (vector<string>::iterator i = vectorName.begin();
                i != vectorName.end();
                ++i)
        {
            cout << *i << endl;
        }
    }
};
  • 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-06-07T02:21:18+00:00Added an answer on June 7, 2026 at 2:21 am

    You need to pass your vector<strings> as a method parameter:

    VectorHelp::showStrings(stringVector);
    
    ...
    
    static void showStrings(const vector<string>& stringVector)
    

    Note that the name of your formal and actual parameter do not need to be the same. Also note that showStrings should be a class member function (i.e. static), not an instance member function.

    Finally, note that printing without loops can be done using facilities of the standard C++ library:

    ostream_iterator<string> out_it (cout, "\n");
    copy (stringVector.begin(), stringVector.end(), out_it);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

During program optimization, trying to optimize a loop that iterates through a vector, I
I am trying to make a sequential animation, where a loop iterates through a
I have a loop that iterates through elements in a list. I am required
in my asp.net mark up I have a foreach loop that iterates through a
Working of foreach: As I know, foreach is a loop which iterates through a
I currently have a loop which iterates through an NSArray of NSString objects. I
For loop should iterate through std::vector and populate content. First for loop gives me
I have a for loop that iterates through an XML document and finds a
In my program I have a while loop which iterates through an array list
I am using a while loop to iterate through a cursor and then outputing

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.