How do I call a function using an array as parameter in C++? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

How do I call a function using an array as parameter in C++?

24th Nov 2018, 8:23 PM
Francois Breedt
6 Antworten
+ 8
The 3rd one of Dennis answer is the common way.. template <typename T, size_t arrLength> <returnType> func(arrayParam){//body } <T> myData[] = {/*...data*/}; func(myData); but it is C style. In C++ I will use std::vector or std::array , they are objects and provide all the necessary methods to work whit inside your body. template <typename T> void print(std::vector<T> data){ for( typename std::vector<T>::iterator it = data.begin() ; it != data.end() ; ++it ) std::cout<< *it ; }
24th Nov 2018, 9:09 PM
AZTECCO
AZTECCO - avatar
+ 6
DarkRanger how can I help you ?
30th Nov 2018, 1:49 PM
AZTECCO
AZTECCO - avatar
+ 6
DarkRanger It's a lot of things to learn quickly! You may start with STL data structures because they are the bricks of DSA, pick one and learn it very well, the others will be easy later because the style and design is the same. Start with vector I say. If you want to develop your own DSA it's another story.. you have to learn manual memory management. Anyway a knowledge of STL design is good to build your own library in a standard fashion. And remember.. c++ and programming is not something you will learn quickly, never trust people saying they have learned it quickly. If quickly lead you to jump of do too big steps then you will live holes in your knowledge.. they will become dooms later when you think you are formed!
28th Dec 2018, 11:10 AM
AZTECCO
AZTECCO - avatar
+ 2
void f( int* arr, size_t size ){ ... } // int arr[100]{}; f( arr, 100 ); void f( int arr[], size_t size ){ ... } // int arr[100]{}; f( arr, 100 ); template<typename T, size_t size> void f( T (&arr)[size] ){ ... } // int arr[100]{}; f( arr ); Pick whichever you like. :)
24th Nov 2018, 8:41 PM
Dennis
Dennis - avatar
0
AZTECCO I need help.
27th Nov 2018, 7:11 PM
DarkRanger
DarkRanger - avatar
0
Hi AZTECCO i had been out of scope i mean could not reply sorry for that but the real problem is i want know stl and DSA quickly how can i make it happen .plz guide me .thnx in advance
28th Dec 2018, 10:16 AM
DarkRanger
DarkRanger - avatar