Let's say I have a couple of vectors of integers and a pointer to a vector of integers. How would I go about changing the value of one element of the pointer to be the address in one of the other vectors of integers?
The context for this is that I've built a class that lets me bring in my desktop into the unreal engine, however on each tick it has to assign a form of vector that contains a struct to the values of another data class each tick, I instead wish to copy the memory addresses over for only a few of the elements (the pixel colour values) so I don't waste time by having to do a copy twice (which for a desktop image is millions of operations)
#include <iostream>
#include <vector>
using namespace std;
// Print function
void PrintVector(vector<int> v)
{
for( int i = 0; i < v.size(); i++ )
{
cout << v[i] << ", ";
}
cout << endl;
}
int main()
{
vector<int> vector1;
vector<int> vector2;
vector<int> *ptrvector;
//Do some assignment so the vectors have values
for( int i = 0; i<3; i++)
{
vector1.push_back(i);
vector2.push_back(2*i);
}
//Assign the pointer to the address of vector1.
ptrvector = &vector1;
//Print out:
PrintVector(vector1); // (1,2,3)
PrintVector(vector2); // (2,4,6)
PrintVector(*ptrvector); // (1,2,3)
// We should see that lines 1 and 3 are the same
//BROKEN BIT::
//Ideally want something like
ptrvector[0] = &vector2[2];
PrintVector(*ptrvector); // (6,2,3);
//Such that if I were to do this:
vector2[2] = 20;
PrintVector(*ptrvector); // It should change as a side effect: (20,2,3)
}
BONUS QUESTION:
Let's say I have this:
TArray<FColor> ColorData;
TArray<FColor> *ptrColorData
//Where TArray is essentially a vector. FColor is a struct with members (R,G,B,A)
//ColorData is initialised somewhere and we set the ptrColorData to the address
ptrColorData = &ColorData;
//Somewhere down the line we have a long loop whereby we do
ColorData[i].B = somedata[i];
ColorData[i].G = somedata[i+1];
ColorData[i].R = somedata[i+3];
//somedata is updated per tick, asigning ColorData per tick as well slows it down.
// I wish to be able to do something on the lines of this
ptrColorData[i].B = &somedata[i];
ptrColorData[i].G = &somedata[i+1];
ptrColorData[i].R = &somedata[i+3];
// this is only called once to initialize. Because the memory address
//is unchanging and somedata changes by functions on its own it means when
// I tell my unreal engine to update a texture by passing (*ptrColorData)
// to a function it automatically has the new data when that function is
// next called.
This should produce the output you want, but be aware. As stated in the comments, the addresses stored int the std::vector<int*> should be considered invalid if you change the size of vector1 or vector2 in any way and if you reorder the elements in the original vectors, your pointers will point to the wrong values. I also removed using namespace std;. See: Why is “using namespace std” considered bad practice?.
#include <iostream>
#include <vector>
// Print function
void PrintVector(std::vector<int> v) {
for(auto x : v) std::cout << x << ", ";
std::cout << "\n";
}
void PrintVector(std::vector<int*> v) {
for(auto x : v) std::cout << *x << ", ";
std::cout << "\n";
}
int main() {
std::vector<int> vector1;
std::vector<int> vector2;
std::vector<int*> ptrvector; // pointers to the elements in vector1/2
//Do some assignment so the vectors have values
for( int i = 1; i<=3; i++) {
vector1.push_back(i);
vector2.push_back(2*i);
}
// Add pointers in ptrvector to the addresses in vector1.
ptrvector.reserve(vector1.size());
for(auto& r : vector1)
ptrvector.emplace_back(&r);
//Print out:
PrintVector(vector1); // (1,2,3)
PrintVector(vector2); // (2,4,6)
PrintVector(ptrvector); // (1,2,3)
ptrvector[0] = &vector2[2];
PrintVector(ptrvector); // (6,2,3);
vector2[2] = 20;
PrintVector(ptrvector); // (20,2,3)
}
Related
I wanted to create a matrix with vectors. In the below code, I created a vector with each entry containing a pointer to another vector(myvector) that acts as columns. I push random values to the myvector (i.e. columns). But when I try to access the values of arrays, it pops an compile error saying "error: no match for 'operator*' (operand type is 'std::vector<int>') at the cout statement. I wonder how do I access the values. I'm pretty sure this is a naive question.
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
std::vector<vector<int>*> main;
for(int j=0; j<3; j++){
vector<int> *myvector = new vector<int>;
main.push_back(myvector);
}
main[0]->push_back(1);
main[0]->push_back(4);
main[1]->push_back(6);
main[1]->push_back(7);
main[1]->push_back(8);
main[2]->push_back(3);
for(int j=0; j<3; j++){
for(uint32_t i=0; i<main[j]->size(); i++)
std::cout<<main[j][i]<<" ";
cout<<"\n";
}
return 0;
}
You could just have done
vector<vector<int>> main;
each index of vector main represents another vector
so to push a number in the first column you can do
main[0].push_back = (_number_)
To access a number in first row in first column we can do main[0][0]
This example shows both the syntax you where looking for, and also an example of how you should use std::vector without new/delete.
#include <iostream>
#include <vector>
#include <memory>
// using namespace std; <== teach yourself NOT to do this.
// https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
void access_pointers_in_2d_vector()
{
std::vector<std::vector<int>*> values; // don't call your variables main!
for (int j = 0; j < 3; j++)
{
std::vector<int>* myvector = new std::vector<int>;
values.push_back(myvector);
}
values[0]->push_back(1);
values[0]->push_back(4);
values[1]->push_back(6);
values[1]->push_back(7);
values[1]->push_back(8);
values[2]->push_back(3);
for (int j = 0; j < 3; j++)
{
for (uint32_t i = 0; i < values[j]->size(); i++)
{
//==================================================================
// this is the syntax you're looking for
// first dereference the pointer then use operator[]
std::cout << (*values[j])[i] << " ";
//==================================================================
}
std::cout << "\n";
}
// don't forget to cleanup your memory!
// if you typed new somewhere then there should
// ALWAYS be a matching delete in your code too!
for (int j = 0; j < 3; j++)
{
delete values[j]; // <<==== !!!!!!!
}
}
// for dynamic memory managment new/delete aren't recommended anymore.
// use std::unique_pointer (or if your design really requires it std::shared_ptr)
void using_unique_pointer()
{
// If you really need pointers, use std::unique_ptr
// it will prevent you from introducing memory leaks
const std::uint32_t size = 3ul;
std::vector<std::unique_ptr<std::vector<int>>> values(size);
for (auto& p : values)
{
p = std::make_unique<std::vector<int>>();
}
values[0]->push_back(1);
values[0]->push_back(4);
values[1]->push_back(6);
values[1]->push_back(7);
values[1]->push_back(8);
values[2]->push_back(3);
// output loop is same as for normal pointers.
// no need to call delete, std::unique_ptr will do that for you
}
void without_pointers()
{
// However your whole code in idiomatic c++ should look like this.
// https://en.cppreference.com/w/cpp/container/vector/vector constructor (10)
// https://en.cppreference.com/w/cpp/language/range-for these loops avoid bugs related to
// letting indices go out of bounds.
std::cout << "\nusing (nested) initializer list and range based for loops : \n";
std::vector<std::vector<int>> rows{ {1,4}, {6,7,8}, {3} };
for (const auto& row : rows)
{
for (const auto& value : row)
{
std::cout << value << " ";
}
std::cout << "\n";
}
}
int main()
{
access_pointers_in_2d_vector();
using_unique_pointer();
without_pointers();
return 0;
}
I'm learning C++ and I'm wondering if anyone can explain some strange behaviour I'm seeing.
I'm currently learning memory management and have been playing around with the following code:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// pass back by pointer (old C++)
const int array_size = 1e6; // determines size of the random number array
vector<int> *RandomNumbers1()
{
vector<int> *random_numbers = new vector<int>[array_size]; // allocate memory on the heap...
for (int i = 0; i < array_size; i++)
{
int b = rand();
(*random_numbers).push_back(b); // ...and fill it with random numbers
}
return random_numbers; // return pointer to heap memory
}
int main (){
vector<int> *random_numbers = RandomNumbers1();
for (int i = 0; i < (*random_numbers).size(); i++){
cout << (*random_numbers)[i] + "\n";
}
delete random_numbers;
}
What I'm trying to do is get a pointer to a vector containing random integers by calling the RandomNumbers1() function, and then print each random number on a new line.
However, when I run the above code, instead of printing out a random number, I get all sorts of random information. It seems as though the code is accessing random places in memory and printing out the contents.
Now I know that I'm doing something stupid here - I have an int and I am adding the string "\n" to it. If I change the code in main() to the following, it works fine:
int main (){
vector<int> *random_numbers = RandomNumbers1();
for (int i = 0; i < (*random_numbers).size(); i++){
cout << to_string((*random_numbers)[i]) + "\n";
}
}
However I just can't understand the behaviour I'm getting with the "wrong" code - i.e. how adding the string "\n" to (*random_numbers)[i]
causes the program to access random areas of memory, instead of where my pointer is pointing to. Surely I have de-referenced the pointer and accessed the element at position i before "adding" "\n" to it? So how is the program instead accessing a totally different memory address?
"\n" is a string literal. It is an array and it is converted to a pointer pointing at its first element in your expression.
(*random_numbers)[i] is an integer.
Adding a pointer to an integer means that advance the pointer by the integer.
This will drive the pointer to out-of-range because "\n" has only 2 elements ('\n' and '\0') but the numbers returnd from the rand() function are likely to be larger than 2.
There are several issues with your code.
you are using delete instead of delete[] to free the array allocated with new[].
you are creating an array of 1000000 vectors, but populating only the 1st vector with 1000000 integers. You probably meant to create just 1 vector instead.
you can and should use the -> operator when accessing an object's members via a pointer. Using the * and . operators will also work, but is more verbose and harder to read/code for.
you are trying to print a "\n" after each number, but you are using the + operator when you should be using the << operator instead. You can't append a string literal to an integer (well, you can, but it will invoke pointer arithmetic and thus the result will not be what you want, as you have seen).
With that said, try something more like this:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int array_size = 1e6; // determines size of the random number array
vector<int>* RandomNumbers1()
{
vector<int> *random_numbers = new vector<int>;
random_numbers->reserve(array_size);
for (int i = 0; i < array_size; ++i)
{
int b = rand();
random_numbers->push_back(b);
}
return random_numbers;
}
int main (){
vector<int> *random_numbers = RandomNumbers1();
for (size_t i = 0; i < random_numbers->size(); ++i){
cout << (*random_numbers)[i] << "\n";
}
/* alternatively:
for (int number : *random_numbers){
cout << number << "\n";
}
*/
delete[] random_numbers;
}
However, if you are going to return a pointer to dynamic memory, you really should wrap it inside a smart pointer like std::unique_ptr or std::shared_ptr, and let it deal with the delete for you:
#include <iostream>
#include <vector>
#include <cmath>
#include <memory>
using namespace std;
const int array_size = 1e6; // determines size of the random number array
unique_ptr<vector<int>> RandomNumbers1()
{
auto random_numbers = make_unique<vector<int>>();
// or: unique_ptr<vector<int>> random_numbers(new vector<int>);
random_numbers->reserve(array_size);
for (int i = 0; i < array_size; ++i)
{
int b = rand();
random_numbers->push_back(b);
}
return random_numbers;
}
int main (){
auto random_numbers = RandomNumbers1();
for (size_t i = 0; i < random_numbers->size(); ++i){
cout << (*random_numbers)[i] << "\n";
}
/* alternatively:
for (int number : *random_numbers){
cout << number << "\n";
}
*/
}
Though, in this case, there is really no good reason to create the vector dynamically at all. 99% of the time, it is unnecessary and unwanted to use standard containers like that. Since the vector manages dynamic memory internally, there is no reason for the vector itself to also be created in dynamic memory. Return the vector by value instead, and let the compiler optimize the return for you.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int array_size = 1e6; // determines size of the random number array
vector<int> RandomNumbers1()
{
vector<int> random_numbers;
random_numbers.reserve(array_size);
for (int i = 0; i < array_size; ++i)
{
int b = rand();
random_numbers.push_back(b);
}
return random_numbers;
}
int main (){
vector<int> random_numbers = RandomNumbers1();
for (size_t i = 0; i < random_numbers.size(); ++i){
cout << random_numbers[i] << "\n";
}
/* alternatively:
for (int number : random_numbers){
cout << number << "\n";
}
*/
}
I would like to check the size of different vectors by iteration using a for loop.
#include <vector>
#include <iostream>
using namespace std;
vector<int> V1;
vector<int> V2;
vector<int> V3;
for (int i=0;i<3;i++){
cout<<Vi.size()<<endl;
}
Unfortunately i don't know if it's possible to iterate through vector names and if it is, how to write the parameter i such that my compiler knows that it is the integer of my loop.
thanks in advance.
There is no way to do what you're attempting to do in that specific way, aka evaluating i into its value for the variable name in Vi.size(), because the compiler evaluates this to a variable named Vi which doesn't exist. However, as others have pointed out you can do this differently but still get the same result, here are a few examples:
Credit to #kerrek-sb for this one (probably the best option):
for (auto* v : {&V1, &V2, &V3}) { std::cout << v->size() << std::endl; }
Using an array (downside: to add more vectors you have to use dynamic allocation and copying; upside: simple for your purposes as shown):
const int size = 3; // Or whatever size you want
// needs to be const because c++11 and up don't support variable length arrays(VLAs)
std::vector<int> V[size];
V[0].push_back(0); // Appends integer to vector
V[0].push_back(10);
V[1].push_back(1);
V[2].push_back(2);
for (int i = 0; i < size; i++) {
std::cout << V[i].size() << std::endl;
} // OUTPUT:
// 2
// 1
// 1
Using an array of pointers to already created vectors(downside: you have to use dynamic allocation to change the size of the array; upside: you can pass vectors into a function and access them like this:
const int size = 3;
std::vector<int> V1(10); // Create three vectors of different sizes
std::vector<int> V2(12);
std::vector<int> V3(14);
std::vector<int> *V[size]; // Create an array of 3 vector<int> pointers
V[0] = &V1; // Set each element to it's corresponding vector
V[1] = &V2;
V[2] = &V3;
for (int i = 0; i < size; i++) {
std::cout << V[i]->size() << std::endl;
} // OUTPUT:
// 10
// 12
// 14
Using a vector of vectors (upside: you can push_back(append) as many vector's as you'd like after creation):
int size = 3; // Or whatever size you want
std::vector<std::vector<int>> V(0);
for (int i = 0; i < size; i++) {
V.push_back(std::vector<int>((i+1)*2)); // (0+1)*2; (1+1)*2; (2+2)*2
}
for (int i = 0; i < V.size(); i++) {
std::cout << V[i].size() << std::endl;
} // OUTPUT:
// 2
// 4
// 6
You could also do a vector of pointers to vector's which would give you both the variable size of a vector and the ability to pass in vectors that are already created from other places.
When I create a stl priority_queue with a customized compare function class, I find that it has very poor performance(It seems to have a O(n) complexity to push a element rather than O(logN)). But I test to use a stl set with the same compare function, the set runs much faster than priority_queue. Why this happened?
//There are a lot of horizontal lines on x-axis. Each line is represeted by a pair<int,int> p,
//where p.first is the left point and p.second is the right point.
//For example, p(3,6) is a line with end points of 3 and 6.
//a vector<pair<int,int>> lines represents all these lines.
//
//I would like to scan all the end points from left to right alone x-axis.
//I choose to use a struct end_point to represent each end point,
// struct end_point{
// int x_coor; //the x coordinate of the end point;
// int index; //the index of the line, this end point belongs to, in vector lines.
// };
//For example, if there are 3 lines on x-axis line1(1,10), line2(2,9) and line3(3,8),
//the vector of lines looks like vector<pair<int,int>> lines = [(1,10), (2,9), (3,8)];
//And the 6 end points look like {1,0}, {10,0}, {2,1}, {9,1}, {3,2}, {8,2}
//I want to sort these end points by comparing their x_coor(if equal, use index to identify different lines).
//If I choose a priority_queue to sort them, it has a very poor performace(6000 μs to complete 1000 lines).
//But I do the same thing(by using the same customized compare function class) on a set,
//it runs much faster(just 246 μs) than priority_queue.
//
//My question:
//1.Why priority_queue perform so bad as to set in this example?
//2.For the comparator function class, I introduce a variable count to record how many times the compare function is called totally.
//but for priority_queue, it seems that each compare function would resume it to 0 without keeping it, while for the set, it could
//increase all the time as I expect. Is there any bug in my code(or that is the reason why there performed so differently)?
//
#include <iostream>
#include <queue>
#include <set>
#include <chrono>
using namespace std::chrono;
//generate 1000 lines of [(0, 10000), (1, 9999), (2,9998),(3,9997).....(999, 9001)]
std::vector<std::pair<int,int>> linesGenerator(){
std::vector<std::pair<int,int>> lines;
int lines_num = 1000;
int length_range = 10000;
for(int i = 0; i < lines_num; i++){
int start = i;
int end = length_range - i;
lines.push_back(std::make_pair(start, end));
}
return lines;
}
struct end_point{
int x_coor; //the x coordinate of the end point;
int index; //the index of the line, this end point belongs to, in vector lines.
end_point(int x, int i):x_coor(x), index(i){}
};
//customized compare function class for priority_queue
class pqCompare{
public:
//construct by keeping its own copy of lines.
pqCompare(std::vector<std::pair<int,int>> &_lines){
lines = _lines;
count = 0; //use for count how many times the compare operation is called.
}
bool operator()(end_point ep1, end_point ep2){
++count; //increase by 1 if this function is called once.
//std::cout<<"count:"<<count<<std::endl;
if(ep1.x_coor != ep2.x_coor){
return ep2.x_coor < ep1.x_coor;
}
return ep1.index > ep2.index;
}
private:
std::vector<std::pair<int,int>> lines;
int count;
};
//customized compare function for set, almost same as priority_queue,
//the difference is only because piroity_queue is a max_heap.
class setCompare{
public:
setCompare(std::vector<std::pair<int,int>> &_lines){
lines = _lines;
count = 0;
}
bool operator()(end_point ep1, end_point ep2){
++count;
//std::cout<<"count:"<<count<<std::endl;
if(ep1.x_coor != ep2.x_coor){
return ep1.x_coor < ep2.x_coor;
}
return ep1.index < ep2.index;
}
private:
std::vector<std::pair<int,int>> lines;
int count;
};
void test_pqueue()
{
//generate 1000 lines;
std::vector<std::pair<int,int>> lines = linesGenerator();
//create a priority_queue with cmp as comparator.
pqCompare cmp(lines);
std::priority_queue<end_point, std::vector<end_point>, pqCompare> ct(cmp);
auto tp0 = steady_clock::now();
for (int i = 0; i < lines.size(); ++i){
//for each line, there are 2 end points.
int left_point = lines[i].first;
int right_point = lines[i].second;
ct.push(end_point(left_point, i));
ct.push(end_point(right_point, i));
}
//std::cout<<"total count"<<cmp.getCount()<<"\n";
auto tp1 = steady_clock::now();
std::cout << __PRETTY_FUNCTION__ << ':' << duration_cast<microseconds>(tp1-tp0).count() << " μs\n";
}
void test_set()
{
std::vector<std::pair<int,int>> lines = linesGenerator();
setCompare cmp(lines);
std::set<end_point, setCompare> ct(cmp);
auto tp0 = steady_clock::now();
for (int i = 0; i < lines.size(); ++i){
int left_point = lines[i].first;
int right_point = lines[i].second;
ct.insert(end_point(left_point, i));
ct.insert(end_point(right_point, i));
}
auto tp1 = steady_clock::now();
std::cout << __PRETTY_FUNCTION__ << ':' << duration_cast<microseconds>(tp1-tp0).count() << " μs\n";
}
int main()
{
test_pqueue();
test_set();
}
#include <iostream>
using namespace std;
int main(){
int findMax(int *);
const int MAX = 100;
int values[MAX];
char ivals[256];
// Get the space-separated values from user input.
cin.getline(ivals, 256, '0');
char *helper;
// Clean input array and transfer it to values.
for(int i = 0; i < (MAX) && ivals[i] != 0; i++){
helper = ivals[i * 2];
values[i] = atoi(helper);
}
int mval = findMax(values);
cout << values << endl << mval;
return 0;
}
//Function to find the maximum value in the array
int findMax(int arr[]){
int localmax = 0;
for(int i = 0; i < (sizeof(arr)/sizeof(int)); i++){
if(arr[i] > localmax){
localmax = arr[i];
}
}
return localmax;
}
The purpose of this program is for the user to input a space-separated series of values ended by a 0. That array is then to be analyzed to find the max. I figured out how to convert what is originally a char[] into an int[] so that I can use the findMax() function on it without error but the sorting loop seems to have a problem of its own and when "cout << values << endl << mval;" is called, it returns only a memory address instead of what should be a non-spaced sequence of ints. Can anybody explain what I am doing wrong? It seems that I may have made some mistake using the pointers but I cannot figure out what.
Printing values won't print the contents of the array as you expect, it will print the memory location of the first element of the array.
Try something like this instead:
#include <iterator>
#include <algorithm>
// ...
copy(&values[0], &values[MAX], ostream_iterator(cout, " "));
Sorry I can't post actual working code, but your original post is a mess with many syntax and syntactic errors.
EDIT: In the interest of being more complete and more approachable & understandable to beginners, I've written a small program that illustrates 4 ways to accomplish this.
Method 1 uses copy with an ostream_iterator as I've done above.
Method 2 below is probably the most basic & easiest to understand.
Method 3 is a C++0x method. I know the question is tagged C++, but I thought it might be educational to add this.
Method 4 is a C++ approach using a vector and for_each. I've implemented a functor that does the dumping.
Share & Enjoy
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
struct dump_val : public unary_function<int,void>
{
void operator()(int val)
{
cout << val << " ";
}
};
int main(){
int vals[5] = {1,2,3,4,5};
// version 1, using std::copy and ostream_iterator
copy(&vals[0], &vals[5], ostream_iterator<int>(cout, " "));
cout << endl;
// version 2, using a simple hand-written loop
for( size_t i = 0; i < 5; ++i )
cout << vals[i] << " ";
cout << endl;
// version 3, using C++0x lambdas
for_each(&vals[0], &vals[5], [](int val)
{
cout << val << " ";
}
);
cout << endl;
// version 4, with elements in a vector and calling a functor from for_each
vector<int> vals_vec;
vals_vec.push_back(1);
vals_vec.push_back(2);
vals_vec.push_back(3);
vals_vec.push_back(4);
vals_vec.push_back(5);
for_each( vals_vec.begin(), vals_vec.end(), dump_val() );
cout << endl;
}
When you pass around an array of X it's really a pointer to an array of X that you're passing around. So when you pass values to cout it only has the pointer to print out.
You really should look into using some of the standard algorithms to make your life simpler.
For example to print all the elements in an array you can just write
std::copy(values, values+MAX, std::ostream_iterator<int>(std::cout, "\n"));
To find the max element you could just write
int mval = *std::max_element(values, values+MAX);
So your code becomes
#include <iostream>
using namespace std;
int main(){
const int MAX = 100;
int values[MAX];
char ivals[256];
// Get the space-separated values from user input.
cin.getline(ivals, 256, '0');
char *helper;
// Clean input array and transfer it to values.
for(int i = 0; i < (MAX) && ivals[i] != 0; i++){
helper = ivals[i * 2];
values[i] = atoi(helper);
}
copy(values, values+MAX, ostream_iterator<int>(cout, "\n"));
cout << *std::max_element(values, values+MAX);
return 0;
}
Doing this removes the need for your findMax method altogether.
I'd also re-write your code so that you use a vector instead of an array. This makes your code even shorter. And you can use stringstream to convert strings to numbers.
Something like this should work and is a lot less code than the original.
int main(){
vector<int> values;
char ivals[256];
// Get the space-separated values from user input.
cin.getline(ivals, 256, '0');
int temp = 0;
stringstream ss(ivals);
//read the next int out of the stream and put it in temp
while(ss >> temp) {
//add temp to the vector of ints
values.push_back(temp);
}
copy(values.begin(), values.end(), ostream_iterator<int>(cout, "\n"));
cout << *std::max_element(values.begin(), values.end());
return 0;
}
Array of int is promoted to a pointer to int when passed to a function. There is no operator << taking ordinary array. If you want to use operator << this way, you need to use std::vector instead.
Note: it is possible technically to distinguish array when passed to a function using template, but this is not implemented for standard operator <<.
for(int i = 0; i < (sizeof(arr)/sizeof(int)); i++){
sizeof(arr) here is the size of the pointer to the array. C++ will not pass the actual array, that would be grossly inefficient. You'd typically only get one pass through the loop. Declare your function like this:
int findMax(int* arr, size_t elements) {
//...
}
But, really, use a vector.
Oh, hang on, the question. Loop through the array and print each individual element.