Load info form txt file into array

This is a discussion on Load info form txt file into array within the C++ Programming forums, part of the General Programming Boards category; It's a simple program I think. It just needs to sort the input that is in a txt file. The ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    Load info form txt file into array

    It's a simple program I think. It just needs to sort the input that is in a txt file.

    The input will be this, for instance: This is is an an icorrect sntence.

    Input Output (on the screen)
    delete("is",2)
    print 1:This 2:is 3:an 4:an 5:icorrect 6:sntence
    delete("an",3)
    print 1:This 2:is 3:an 4:icorrect 5:sntence
    delete("icorrect",4)
    print 1:This 2:is 3:an 4:sntence
    insert("incorrect",4)
    print 1:This 2:is 3:an 4:incorrect 5:sntence
    delete("sntence",5)
    insert("sentence",5)
    print 1:This 2:is 3:an 4:incorrect 5:sentence
    neighbors("is") 2:is previous:This next:an
    And the output will be: This is a correct sentence.

    I'm wanting to use array-based lists/linked lists. How do I load the input into the list?

    Code:
    vector<string> words;
    string word;
    ifstream fin("text.txt") // use ifstream object to get info from file
    while (fin >> word) // will read until there is no more info in the file
    words.push_back(word); // add the word to the vector
    Thanks !

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    well, lists are not array based. They have slow random access to each element. Vectors will give you faster random access. Lists are good for fast erasing and fast insertion.

    In your code, just replace
    vector<string> words;

    with list<string> words;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Separate info in a txt file and put it into variables
    By Netflyer in forum C Programming
    Replies: 13
    Last Post: 12-25-2007, 09:11 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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