phew! I do honestly appreciate the effort you put up to try and help me but man we are really drifting apart here!!
yes I do know that
# C arrays are not different from C++ arrays
# an array is a collection of elements continguous in memory
# You use the [] to define ...
# int arr1[5][3] has 5 elements not 15
# The name of the array is always the address of element 0
# Passing arrays to functions incurs decay of array
# a vector is implement via an array. im using vectors = im using arrays
the most relevant difference between array and vector in my opinion is that a vector knows about its size and i can truely forget about the fact that there is an array living inside of the vector. this really enables me to not pay any more attention to the man(array) behind the curtain(vector)!
Yes I could - bypass the vector methods and work with the array like I did in C. - but I do desperately NOT want to!
I do understand that IF i would be using an int* and overloading then the signatures of the two doit's would differ:
# doit(int* arr, int dim)
# doit(int* arr, int dim1, int dim2)
and that would work.
The point is that
a) I dont want to use arrays, since they offer no benefit over vector (but the contrary!)
b) I can not use doit(int* arr, int dim1, int dim2), because I do frequently face the situation that my datastructure is not rectangular, but like in the example of
- std::vector< std::vector<double > > v2;
-
v2.push_back( std::vector<double>(2) );
-
v2.push_back( std::vector<double>(3) );
the 'rows' in the 'matrix' v2 do not all have the same number of entries.
uff.
Maybe I really caused this missunderstanding by my use of the word array - the problem is that im really running out of words here. so let me propose the following notation
# c-array
denotes datastructures like
int[3] a;
as they occur in C/C++ programming language
# vector
denotes the datastructure provided by STL: std::vector<>
# array
denotes the concept of a datastructure that allows constant time access to a random element. That concept occurs all through out computer science and i guess any selfrespectable programming language provides something like that.
c-array's and vector's are examples of that. just like eg java.util.Vector
From now on I will strictly stick to that notation.
So im trying to come back to my question.
Im using std::vector and I would want to provide functions that perform some operation on all elements of a vector<int> or on all elements of vector<vector<int> >.
I have achieved that by using different names: doit and doitm
- // for array of val
-
template< typename ARRAY, typename VAL >
-
void doit(ARRAY& arr, VAL v)
-
{
-
for ( unsigned int i = 0 ; i < size(arr); i++ )
-
{
-
arr[i] = v;
-
cout << arr[i] << " ";
-
}
-
cout << "\n";
-
}
-
-
// for array of array
-
template<typename ARRAY, typename VAL>
-
void doitm(ARRAY& arr, VAL v)
-
{
-
for ( unsigned int i=0; i < size(arr); i++ ){
-
doit(arr[i],v);
-
}
-
}
-
Im wondering whether template specialization would offer a way to use the name doit for both functions.
Or maybe someone has any other suggestion that is superior to the doit/doitm approach, whereas i dont think a doit(int* arr, int dim)/doit(int* arr, int dim1, int dim2) is superior to doit/doitm.