Комментарии:
Challenge accepted:
Use for each or just calculate size of the array. I'm I stupid and I'm not understanding something or what?
Void printArray( int* arr)
{
for (auto i: arr) serial.println(i);
// or
For (int I = 0; I < sizeof(arr) / sizeof(int); i++)
//Int is default cuz this is parameter type, u can make template out of it
}
I wrote that:
template<int T>
void PrintArray(std::array<int, T>& arr)
{
for (int i = 0; i < arr.size(); i++)
{
std::cout << arr[i] << '\n';
}
}
template<int S,typename T> void foo(std::array<T,S>){...}
Ответитьit totally can work pretty well。
Ответитьtemplate<class T,int sizee>
void func(const std::array<T,sizee>& arr) {
cout << arr.size();
}
void PrintArray(const auto& data) {
for (const auto& value : data) {
std::cout << value << " ";
}
std::cout << std::endl;
}
or
template <typename T, size_t N>
void PrintArray(const std::array<T, N>& data) {
for (const T& value : data) {
std::cout << value << " ";
}
std::cout << std::endl;
}
edit: after C++17 and later you can use auto, however you lose some type safety and intent clarity, as well as being able to tell error messages correctly. I prefer the template way, however both are correct.
35 degrees is cold bro
Ответитьbut the issue is we should know the size at compile time, if we don't know the size at compile time, we cannot specify the size in template argument.
ОтветитьThe array size problem can be solved using templates
Ответитьbro how is it hot its Jan u should be freezing
Ответитьexplanation to your question:
We use template to get the data and size
template<class T, size_t size>
void print( const std::array<T, size> & Data)
{
for ( T i : Data)
std::cout << i << std::endl;
}
int main()
{
std::array<int, 5> Number;
Number[0] = 10;
Number[1] = 12;
Number[2] = 13;
Number[3] = 19;
Number[4] = 0;
print(Number);
}
hope u got aircondition after 5 years of hard work
ОтветитьStatic arrays (std::array) are just an array which you can't change the value of and size of.
ОтветитьYeah a huge problem with C++ programmers is that the majority of them are not programming in C++, but instead in C without even knowing it. If you're writing C++ then write C++ code, otherwise just go and write C. No one in C++ should be creating C style arrays, youre using C++ so do it the C++ way and use std::Array instead.
ОтветитьYou can set the size with a template
ОтветитьThe solution is auto, like explained in the previous video! So, "const auto& data"!
ОтветитьGoing over bounds in a c-style array gave me seg fault core dump
ОтветитьI think that if you don't know the size, you can either use a template or a macro but I think you have explicitly said that macros are mostly bad except for debugging and too much templating can become dubious. Based on my current knowledge I would probably use a template.
Ответитьtemplate<typename T, size_t X>
void PrintArray(const std::array<T, X>& array)
{
for(int i = 0; i < X; i++)
{
std::cout << array[i] << std::endl;
}
}
template <std::size_t SIZE>
void PrintArray(const std::array<int, SIZE>& array) {
for (std::size_t i = 0; i < SIZE; i++) {
std::cout << array[i] << std::endl;
}
}
I don’t think std::arrays do bounds checking using the overloaded [] operator. You would have to use the `at(i)` method instead of [i].
Ответитьtemplates right away answer thanks to you man
ОтветитьIf you pass in an std::array then you should be able to just get the size from that right
ОтветитьYou can pass the address of std::array as void* ptr in function argument and by knowning the offset of size inside array class you can find it like this: int size = *(int*)(uintptr_t(ptr) + *size_offset*)
ОтветитьThank you, i am grateful that there is person such you. It really helps to figure out in c++
ОтветитьIt's worth noting that the size() function is also constexpr, so it doesn't actually return 5, but rather the compiler will just replace the function call itself with 5, so something like:
int s = ary.size();
literally becomes
int s = 5;
with no function call at runtime.
To pass an unsized std::array to a function the function must be templated with the size parameter as a template parameter.
Which is pretty gross because it forces your code into the header and makes a lot of things rigid when you're using it as a class member, ets. This is the main reason that I usually don't bother with std::array and just go with a vector instead, even if the size is constant.
Use size_t for sizes, you heathen.
ОтветитьThe best solution with C++ 20 ➡
void PrintArray (const std::span<int>& data) {
for (auto& : data) {
std::cout << data << std::endl;
}
// Or
for (int i = 0; i < data.size(); i++) {
std::cout << data[i] << std::endl;
}
}
I looked at the date 18 January.
Then you said that it's 40 degrees outside.
Then I remembered that you live in Australia.
Hi, if std::array is stored on stack, how is this code valid?
std::array<int, 3> f() {
return std::array<int, 3> {1, 2, 3};
}
Answer is by template :
template<typename T, std::size_t size>
void printArray(const std::array< T, size >&data)
{
// now we don’t need to pass type and size explicitly
}
Isn't adding too many std::array with varying sizes (say for the same data type) will increase the size of program memory (code segment)? The size stored as a literal in the compiled program and that template is for every size type at the cost of program size.
ОтветитьIm aiming to be a coder. Started this year. I wonder how long it will take me to get my first job?
Ответитьauto or template
Ответитьi'm screwed. i dislike the russian captions. help me. its so annoying
ОтветитьIt's weired .why the captions were Russian.
?Omg, is google brain smashed?
Cherno help me. I wanna English captions.
late to the party, but I'd go about the problem as below:
template<int size>
void printArray(std::array<int, size>& data) {
for (int i = 0; i <data.size(); ++i) {
LOG(data[i]);
}
}
int main() {
std::array<int, 5> data;
...
printArray<data.size()>(data);
...
}
so i came back from stack overflow and looks like you can't pass this shit::array to function without specifying size. You either have to declare function as a template or specify size in every function, which basically same tracking as with c-array, so what's the point? Just another burden crap
ОтветитьTo the people suggesting templates with std::array, why not
template<int N>
void PrintArray(int *array) {
for(int i=0; i<N; i++) {
}
}
Forty degree, finally a sane person that doesn't use square feet by crate inch type of unit !
Ответитьtemplate <typename T>
void PrintArray(const T& arr){
for (int i=0; i<arr.size();i++){
cout <<arr[i] << endl;
}
}
Man, that heat lol
ОтветитьAt the beginning you're like Eric Andre, but.. but we missed whole intro!
Ответитьtemplate<typename array_type>
void PrintArray(const array_type& m_array)
{
cout << "Array Contents:" << endl;
for (auto& i : m_array)
{
cout << i << endl;
}
}