Char Separation in a vector

This is a discussion on Char Separation in a vector within the C++ Programming forums, part of the General Programming Boards category; Ok don't think this will be too tough but I haven't used vectors very much. I have a vector declared ...

  1. #1
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532

    Char Separation in a vector

    Ok don't think this will be too tough but I haven't used vectors very much. I have a vector declared like this(also an iterator)
    Code:
    vector<string> wordlist;
    vector<string>::iterator forward;
    I retrieve words from a list and need to compare certain letters from a char variable I have to words that would be stored in my vector. Like if you take input into a char buffer[256] variable the buffer[0] will equal the first letter? I need to do that with items in my vector. Could I initialize a char vector instead and use the same way? Like this:
    Code:
    vector<char> wordlist;
    vector<char>::iterator forward;
      //retrieve and place words into vector
    for(forward = wordlist.begin(); forward != wordlist.end(); forward++)
    {
    if(aChar==forward[0])
    {
      //procede by finishing up
    }
    }
    But if I do this then each slot in the vector will only be a letter and I can't retrieve whole words. How could I isolate single characters out of a vector? Thanks.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Have you considered using std::string instead?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    How would that help me? I need to compare two different characters, and to get one of these characters I need to pull them out of words I store into a vector.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Heh, sorry, I realised that I didnt read your question fully - you have already tried working with std::string.

    With your vector of strings, why cant you use (*forward)[0] or forward->at(0)?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Ok, thanks. That's what I needed.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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