0

I have two questions:

1- How can print the values of a for the code stated below???

#include <iostream>
#include <list>
using namespace std;

int main()
{
    int i;
    list<int> a;

    for(i; i<5; i++)
    {
        a.push_back(i);
    }


    system("pause");
    return 0;
}

2- How can I add new element to an integer array? For example, I have an array such that:

int *a;
int size = 3
a = new int [size];

a[0]=0;
a[1]=1;
a[2]=2;

//Now, I would like to add a new element to my array by increasing its size by one

size += size; // new size is 4

How can I use my array with the new size and add a new element to the end of the array?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Taner
  • 11

3 Answers3

2

(1)

list<int>::const_iterator it;

for (it = a.begin(); it != a.end(); ++it)
    cout << *it << endl;

(2) Create a 2nd array with new[] and copy over the first. Remember to delete[] the original. Or, better yet, get rid of the array and use a vector and just push back the new element(s). Vectors automatically resize themselves.

Duck
  • 26,924
  • 5
  • 64
  • 92
  • @Chad : Actually the *proper* way here would have been to use `const_iterator` rather than `iterator`, and prefix increment rather than postfix. ;-] – ildjarn May 08 '11 at 00:45
  • @ildjarn - True but I think those subtleties are lost at this level of question. I'll change anyway. – Duck May 08 '11 at 01:00
  • 1
    Never use `std::endl` when you mean `'\n'`. http://stackoverflow.com/questions/5492380/what-is-the-c-iostream-endl-fiasco – Robᵩ May 08 '11 at 01:29
  • And, never *ever* use `realloc` on a pointer returned by `new[]`. – Robᵩ May 08 '11 at 01:30
  • @Rob - Are you done stating the obvious? I'll take out the realloc because I didn't notice OP used new on the original array. Endl stays because I wanted the buffer flushed so there would be no doubt OP would see the output. – Duck May 08 '11 at 01:45
1
  1. You should initialize i to 0 in the for() loop.
    To print the values you have to make another for() loop but instead of adding elements to the list you print them to cout.

    for(...)
    {
    // print to cout
    }

  2. int* b = new int[size + 1];
    The you should use memcpy() to copy a into b, and then add the last element to b.
    At the end you should call delete for a and b also.

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • But, bear in mind that `memcpy` can only be used on [POD types](http://stackoverflow.com/questions/146452/what-are-pod-types-in-c). – Robᵩ May 08 '11 at 01:33
1

1. Prefer std algorithms to hand-coded for loops:

std::copy(list.begin(), list.end(),
  std::ostream_iterator<int>(std::cout, "\n"));

If hand-coding the loop is still the right choice, consider Boost.Foreach:

BOOST_FOREACH(int i, list) {
  std::cout << i << "\n";
}

2 This is one reason why you should use std containers instead of pointers and arrays. Let somebody else debug the hard code.

int* newa = new int[newsize];
std::copy(a, a+std::min(newsize, size), newa);
delete[] a;
a = newa;
size = newsize;

But, does this code leave the extra items initialized or uninitialized? What exception guarantees does this code offer? Better to skip the new altogether and just declare a std::vector.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308