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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:26:26+00:00 2026-05-25T23:26:26+00:00

hi guys i will show three codes 1 and 2 makes same work but

  • 0

hi guys i will show three codes 1 and 2 makes same work but the third one doesnt work. I want to understand why doesnt work or why do work the other two ? (strrand function produces random string)

1.

int main(){
    vector<string> svec(50);
    randomize();

    generate_n(svec.begin(), 20, strrand);
    display(svec.begin(), svec.end());

    return 0;
}

2.

int main() {
    vector<string> svec;
    randomize();

    generate_n(back_inserter(svec), 20, strrand);
    display(svec.begin(), svec.end());

    return 0;
}

3.

int main(){
    vector<string> svec;
    randomize();

    generate_n(svec.begin(), 20, strrand);
    display(svec.begin(), svec.end());

    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-25T23:26:27+00:00Added an answer on May 25, 2026 at 11:26 pm

    The third has undefined behavior. In the first, you specify the vector size where you define the vector. That means it starts as a vector of 50 default-initialized (empty) strings. You then overwrite those strings with your random strings.

    In the second, you use a back_insert_iterator to add the strings to the vector individually.

    In the third, you start with an empty vector, and you attempt to use the (invalid) iterator to its (nonexistent) beginning. You then write 20 strings starting at whatever spot in memory its (random) initial value happens to refer to. You have not, however, at any time actually inserted a string into the vector. A vector normally keeps a count of how many items it current contains; in your third case, that will start out 0, and remain 0 throughout. When you attempt to show the “contents”, you should get nothing (though, since you’ve already had undefined behavior at that point, anything is possible — especially if some of the data you wrote happened to overwrite part of the vector’s internal data.

    What you have is a marginally more subtle (but equally problematic) version of well known beginner mistakes like:

    char *s;
    
    strcpy(s, "This will give UB");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys so im building an application and one of teh features it will
I know I could call alert('Warning1');alert('Warning2'); and it will show 2 alerts. But, when
I have an odd error that you guys will hopefully be able to help
This will be simple for you guys: var uri = new Uri(pack://application:,,,/LiftExperiment;component/pics/outside/elevator.jpg); imageBitmap =
Guys, I’ve been writing code for 15+ years, but managed to avoid Web Development
guys please post me an example for threading in MFC....it should show the progress
I am try to create a JSP page that will show all the status
I'm not pretty sure if this question is for here, but I want to
I want to ask how other programmers are producing Dynamic SQL strings for execution
Hey guys i want to get the latitude and longitude of the users location.

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.