answersLogoWhite

0

In C++, it is better to use vectors than dynamic arrays because a vector always knows its own size. C-style arrays are better suited to static arrays where the size is always known in advance. The following demonstrates how a vector might be used within a class.

#include<iostream>

#include<vector>

class foo

{

public:

std::vector<int>& operator+= (int data){ m_data.push_back(data); return(m_data);}

int operator[] (size_t element)const{

ASSERT(element<m_array.size());

return( m_array[element]; }

const size_t size()const{return(m_data.size());}

private:

std::vector<int> m_data;

}

int main()

{

foo f;

f += 42;

f += 9;

for(int i=0; i<f.size(); ++i)

std::cout<<f[i]<<std::endl;

}

Output:

42

9

User Avatar

Wiki User

11y ago

Still curious? Ask our experts.

Chat with our AI personalities

MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
More answers

#include <iostream>

using std::cout;

using std::endl;

...

int main()

{

const int arraySize = 5;

double myArray [arraySize] = {0.0};

//Enter elements

...

//Display elements

for (int i = 0; i < arraySize; i++)

{

cout << myArray[i] << endl;

}

...

}

User Avatar

Wiki User

14y ago
User Avatar

Add your answer:

Earn +20 pts
Q: HOW to display array elements C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp