C++ Create a generic function with an array as parameter

I'm new to C++. I'm a student coming from C.

My goal is to write a simple function that takes an array in parameter. I succeed but i'm not happy with the result.

main.cpp

#include <array>
#include <iostream>
#include "MyMath.hpp"

int main()
{
     std::array<int, 5> array = {1, 2, 3, 5, 4};

     MyMath::sortIntArray(array);
     std::cout << array.at(3) << std::endl;
     return 0:
}

MyMath.hpp

#include <array>

 class MyMath {
 public:
     static void sortIntArray(std::array<int, 5> &array)
     {
         array.at(3) = 99;
     }
 }

I created a class to contain my function because that's the school's coding style. The problem that I have with the code above is that I wrote the size of my array in my function's parameter. "What if I want to sort an array of 500 numbers ?"

1) So my question is : what is the best way to write the same function without having to write the array size ?

2) I've done some research and I know about std::vector but isn't it more appropriate to use std::array when you want to create an array with fixed size ?

3) I could solve my issue with C-Style array : by setting the last element of the array at NULL or by sending the size as an second parameter. But isn't more appropriate to use class equivalent for arrays and string ? (std::array / std::vector / std::string and almost never use int[] anymore)

4) I have seen "templates" that would allow me to success my goal but after writting the second little function of my sorting algorithm, I noticed that I would have to use template for every function that takes an std::array as parameter which is crazy.

If the answer is to simply use std::vector or int[]. Will I ever need std::array ? (because it is not better than int[] in my opinion, I could solve the problem with int[] as I said in 3). And I cannot solve the problem with std::array)

There are 4 questions. For my general undestanding of this langage, I think it is important to consider answering all 4 questions (even if it's only a very short sentence). Thanks you in advance.

Look at the interface of std::sort. Instead of taking a concrete container type, it instead takes a generic [start, end) iterator pair. Then if the caller wants to sort a vector, they can pass in a pair of std::vector<int>::iterator_types. If they want to sort an array of 5 ints, they can pass in std::array<int, 5>::iterator_type. If they want to sort an C-style array, they can pass in int*s.

Why C is so Influential - Computerphile, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: 18 Aug 2015 C or c is the third letter in the English and ISO basic Latin alphabets.Its name in English is cee (pronounced / ˈ s iː /), plural cees.

1) So my question is : what is the best way to write the same function without having to write the array size ?

There isn't a best way, it depends on what you need to do.

If you want to use std::array like that, you will have to use a template.

If you don't need the size at compile-time, you have more a few more options, e.g. the ones you describe later.

2) I've done some research and I know about std::vector but isn't it more appropriate to use std::array when you want to create an array with fixed size ?

Yes, it is. They aren't equivalent solutions and they are meant for different use cases.

3) I could solve my issue with C-Style array : by setting the last element of the array at NULL or by sending the size as an second parameter. But isn't more appropriate to use class equivalent for arrays and string ? (std::array / std::vector / std::string and almost never use int[] anymore)

It depends. Again, remember that using those solutions are not equivalent, because size won't be known at compile-time!

4) I have seen "templates" that would allow me to success my goal but after writting the second little function of my sorting algorithm, I noticed that I would have to use template for every function that takes an std::array as parameter which is crazy.

What do you mean by "crazy"?

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: 15 Aug 2018 Ç or ç is a Latin script letter, used in the Albanian, Azerbaijani, Manx, Tatar, Turkish, Turkmen, Kurdish, Zazaki, and Romance alphabets. Romance languages that use this letter include Catalan, French, Friulian, Ligurian, Occitan, and Portuguese as a variant of the letter C. It is also occasionally used in Crimean Tatar and in Tajik to represent the /d͡ʒ/ sound. It is often retained in the spelling of loanwords from any of these languages in English, Basque, Dutch, Spanish and other

std::array is typically the more C++ way of handling arrays whose sizes are fixed and known at compile time, rather than a C-style array. At least one benefit over the old array is ease of copying; a single assignment from one array to another, rather than an element-by-element copy, and helps combat accidental shallow copies.

If you don't know the size at compile time, use std::vector. You can use it's resize method once you know the actual size, and then treat it the same as you would an array or array (i.e., indexing with []).

C Programming Tutorial for Beginners, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone  Stock analysis for Citigroup Inc (C:New York) including stock price, stock chart, company news, key statistics, fundamentals and company profile.

C Tutorial, C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C-- (pronounced cee minus minus) is a C -like programming language. Its creators, functional programming researchers Simon Peyton Jones and Norman Ramsey, designed it to be generated mainly by compilers for very high-level languages rather than written by human programmers.

Learn C Programming, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.

Learn C, What is C++?. C++ is a cross-platform language that can be used to create high-​performance applications. C++ was developed by Bjarne Stroustrup, as an  As well as C and Simula's influences, other languages also influenced this new language, including ALGOL 68, Ada, CLU and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining and default arguments.

Comments
  • Maybe a std::vector and size determined at runtime? As you would intern the std::vector you'll have full control over it. Also you should try to ask one question per question here BTW.
  • Take an iterator pair like std::sort does?
  • You have covered the options as you see them well, but strongly consider the option 5 offered by @NathanOliver as it makes the problem go away by concealing the container behind a layer of abstraction and providing much more versatility.
  • All functions in the standard library that deals with containers are generally templates. I would call it idiomatic, not crazy.