Thread: processing dynamic arrays

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    processing dynamic arrays

    Hello all,

    While learning about dynamic arrays, I'm asked by C++ primer to do the following exercise:

    Write a program to read the standard input and build a vector of ints from values that are read. Allocate an array of the same size as the vector and copy the elements from the vector into the array.
    I came up with what you can see below. I would like to know, is it possible to simplify the for loop where I assign to the dynamic array? Specifically, can I do this without creating a pointer to the dynamic array while making sure I do not overflow?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    #include <iterator>
    
    int main()
    {
        using namespace std;
        
        int input;
        vector<int> iv;
    
        while (cin >> input) {
            iv.push_back(input);
        }
        cout << endl;
    
        for (vector<int>::const_iterator i = iv.begin(); i != iv.end(); ++i) {
            cout << *i << "\t";
        }
        cout << endl;
    
        int *p = new int[iv.size()];
        vector<int>::size_type i = 0;
        for (int *pp = p; pp != p + iv.size(); ++pp, ++i) {
            *pp = iv[i];
            cout << *(p + i) << "\t";
        }
        cout << endl;
    
        delete[] p;
    
    
        system("PAUSE");
    	return 0;
    }
    Last edited by Mario F.; 06-04-2006 at 09:49 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you don't know the size of the array at compile time you will have to dynamically allocate it.
    But you could use copy from <algorithm> to fill the array
    Code:
    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
        using namespace std;
        
        int input;
        vector<int> iv;
    
        while (cin >> input) {
            iv.push_back(input);
        }
        cout << endl;
    
        for (vector<int>::const_iterator i = iv.begin(); i != iv.end(); ++i)
            cout << *i << "\t" ;
        
        cout << endl;
    
        int *p = new int[iv.size()];
        
        copy(iv.begin(), iv.end(), p );
        
        for ( int i = 0; i < iv.size(); ++i )
            cout << p[i] << "\t" ;
        cout << endl;
    
        delete[] p;
    
        return 0;
    }
    Kurt
    Last edited by ZuK; 06-04-2006 at 10:27 AM.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Um, you could use array indices instead of the pointer. You could assign things to the array when you are printing out the vector. I don't know. This is kind of frivolous.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Not sure if you are asking if you can do it without creating a "dynamic" array or not, but in case you are asking if you can do it via a static array you can simply just do:

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
    	vector<int> ivec;
    	int input;
    
    	while (cin >> input) {
    			ivec.push_back(input);
    	}
    
    	int iarray[ivec.size()];
    	copy(ivec.begin(), ivec.end(), iarray);
    
    	// test: print
    	for (int i = 0; i < ivec.size(); i++) {
    			cout << iarray[i] << endl;
    	}
    
    	return 0;
    }
    Not sure what your asking though
    The cost of software maintenance increases with the square of the programmer's creativity.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by 0rion
    Not sure if you are asking if you can do it without creating a "dynamic" array or not, but in case you are asking if you can do it via a static array you can simply just do
    That works with gnu compiler only. std C++ doesn't allow that.
    compile with
    Code:
    g++ ex.cc --pedantic
    Kurt

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I reread my question after the frivolous adjective has been given to it.

    You are right, as was Tonto. It's a confusing question. And more so because I didn't even remember that *(p + i) yields an lvalue. As such, the final loop could have been replaced with...

    Code:
        for (vector<int>::size_type i = 0; i != iv.size(); ++i) {
            *(p + i) = iv[i];
            cout << *(p + i) << "\t";
        }
    Much simpler...
    Dunno, I drew a blank or something. Couldn't see that when I was answering the exercise.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > *(p + i) = iv[i];
    Why not
    Code:
    p[i] = iv[i];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    hmm... because i'm a newb, that's why

    You are right. Since I allocated it, I didn't remember that I couldn't simply access it by name without dereferencing it.

    Ugh... arrays and pointers are killing me!
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM