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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:10:14+00:00 2026-05-11T16:10:14+00:00

I was under the assumption that STL functions could be used only with STL

  • 0

I was under the assumption that STL functions could be used only with STL data containers (like vector) until I saw this piece of code:

#include <functional>
#include <iostream>
#include <numeric>
using namespace std;

int main()
{
    int a[] = {9, 8, 7};
    cerr << "Sum: " << accumulate(&a[0], &a[3], 0, plus<int>()) << endl;
    return 0;
}

It compiles and runs without any warnings or errors with g++, giving the correct output sum of 24.

Is such usage of arrays with STL functions allowed by the C++/STL standard? If yes, how do archaic structures like arrays fit into the grand STL plan of templated iterators, containers and functions? Also, are there any caveats or details in such usage that the programmer should be careful about?

  • 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-11T16:10:14+00:00Added an answer on May 11, 2026 at 4:10 pm

    Well, you ask about an array. You can just easily get a pointer to its elements, so it basically boils down to the question whether pointers can be used transparently with STL functions. A pointer actually is the most powerful kind of an iterator. There are different kinds

    • Input iterator: Only forward and one-pass, and only read
    • Output iterator: Only forward and one-pass, and only write

    • Forward iterator: Only forward, and read/write
    • Bidirectional iterator: Forward and backward, and read/write
    • Random access iterator: Arbitrary steps forward and backward in one breath, and read/write

    Now each iterator in the second group supports all the things of all iterators mentioned before it. A pointer models the last kind of iterators – a random access iterator. You may add/subtract an arbitrary integer and you may read and write. And all except the output iterator has a operator-> that can be used to access a member of the element type we iterate over.

    Normally, iterators have several typedefs as members

    • value_type – what the iterator iterates over (int, bool, string, …)
    • reference – reference to the value_type
    • pointer – pointer to the value_type
    • difference_type – what type the distance between two iterators has (returned by std::distance).
    • iterator_category – this is a tag-type: it is typedefed to a type that represents the kind of the iterator. either std::input_iterator_tag, …, std::random_access_iterator_tag. Algorithms can use it to overload on different kinds of iterators (like std::distance is faster for random access iterators, because it can just return a - b)

    Now, a pointer of course does not have those members. C++ has an iterator_traits template and specializes it for pointers. So if you want to get the value type of any iterator, you do

    iterator_traits<T>::value_type
    

    And whether it is a pointer or some other iterator, it will give you the value_type of that iterator.

    So – yes, a pointer can very well be used with STL algorithms. As someone else mentioned, even std::vector<T>::iterator can be a T*. A pointer is a very good example of an iterator even. Because it is so exceedingly simple but at the same time so powerful that it can iterate over a range.

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

Sidebar

Related Questions

A previous LOAD DATA INFILE was run under the assumption that the CSV file
I was under the assumption that an anonymous user cannot send a presence stanza
I have been under the assumption for a while that viewDidUnload is always called
I wrote the following code under the assumption that Excel will die with Monkey:
I was under the assumption that a print stylesheet would not get loaded when
I'm under the assumption that INT(1) is the exact same thing as TINYINT(1) but
I am currently working under the assumption that -performSelector:withObject:afterDelay: does not utilize threading, but
I had been programming under the assumption that, when calling a method in C#
I'm developing an indie video game, and have been operating under the assumption that
So, I have some JavaScript/jQuery that looks like this: var $foo = $('#bar'); $foo.hide();

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.