Thread: processing dynamic arrays

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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

  3. #3
    (?<!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.

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