Reputation: 59
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.
Upvotes: 1
Views: 1743
Reputation: 431
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_type
s. 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.
Upvotes: 2
Reputation: 1619
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 []
).
Upvotes: 1
Reputation: 26186
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"?
Upvotes: 1