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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:19:54+00:00 2026-05-30T15:19:54+00:00

Maybe this is a very silly question, but the book I’m reading instructed me

  • 0

Maybe this is a very silly question, but the book I’m reading instructed me to write a piece of code that uses algorithms to scramble and order the elements in a vector. To do this the book tells me to use the algorithms library from the main C++ library. Alright, so far I understand it, but after writing the code I wanted to see what would break if I would remove this library from the top-part of my code, and it surprised me that everything still worked.

This is the code I’m talking about. When I remove ‘#include algorithm’ from the top-part of the code, nothing breaks. How can this be? Isn’t the ‘random_shuffle’ part supposed to break when not using this library?

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    vector<int>::const_iterator iter;

    cout << "Creating a list of scores.";
    vector<int> scores;
    scores.push_back(1500);
    scores.push_back(3500);
    scores.push_back(7500);

    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
    {
        cout << *iter << endl;
    }

    cout << "\nFinding a score.";
    int score;
    cout << "\nEnter a score to find: ";
    cin >> score;
    iter = find(scores.begin(), scores.end(), score);
    if (iter != scores.end())
    {
        cout << "Score found.\n";
    }
    else
    {
        cout << "Score not found.\n";
    }

    cout << "\nRandomizing scores.";
    srand(static_cast<unsigned int>(time(0)));
    random_shuffle(scores.begin(), scores.end());
    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
    {
        cout << *iter << endl;
    }

    cout << "\nSorting scores.";
    sort(scores.begin(), scores.end());
    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
    {
        cout << *iter << endl;
    }

    system("pause");
    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-30T15:19:56+00:00Added an answer on May 30, 2026 at 3:19 pm

    The reason it works is because has been included by a header that you have also included.

    For example vector might have included algorithms in it’s source. This is common as they are often header only.

    That said, you can not rely on the specific implementation of the standard library to have the same includes in each header. (for example with might work with MSVC and it might break with gcc stdlibc+++).

    For this reasons I highly recommend including what you use, regardless of where it will compile of not. — note that this is slightly different to ‘what you reference’ because forward declaration for point and references in headers can significantly improve build time.

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

Sidebar

Related Questions

This may be a silly question, but googling that is not very effective. And
Maybe this is a very silly question but I'm new using django. I have
Maybe this is a silly question, but I am very new to PDO and
Maybe a very simple question. How can I put in this code <Query> <Where>
My question may be very silly but here it is. I went through this
Maybe this is a very stupid question but... Why should I use tinytext and
I know that this is a very commonly asked question, but all of the
Maybe this is a silly problem with a very simple solution but still is
maybe this is a very simple question, but forgot the answer about it because
Maybe it is a silly question but I am a very confused with JavaScript.

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.