more efficient code

This is a discussion on more efficient code within the C++ Programming forums, part of the General Programming Boards category; Is it better to use arrays, list, or vector for the below code? It works but I am just not ...

  1. #1
    Unregistered
    Guest

    Question more efficient code

    Is it better to use arrays, list, or vector for the below code? It works but I am just not sure of the most efficient way to do it:

    //reverse print out of file read
    #include <iostream> //cout and cin
    #include <string> //used to manipulate strings
    #include <fstream> //used to open files for reading and writing
    #include <vector>
    using namespace std;

    int main()
    {
    vector<string> v;
    ifstream in("vector.txt");
    string line;
    while(getline(in,line))
    v.push_back(line); //add the line to the end
    //add line numbers
    vector<string>::reverse_iterator v2;
    for (v2 = v.rbegin(); v2 != v.rend(); v2++)
    cout << *v2 << endl;
    }

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    The linked list or the vector in the stl will be good. For speed ups
    on the vector you could pass in the a estimate of the number
    of lines you think your going to read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  2. How efficient is this code?
    By Morgan in forum C Programming
    Replies: 6
    Last Post: 05-23-2003, 04:47 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. Debugging leads to buggy code and longer hours?
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 10:14 AM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21