little help with vectors... (c++)

Walleye

Limp Gawd
Joined
Jan 13, 2004
Messages
432
alright... i'm looking for a more elegant way of doing this than what i have in mind currently.

okay.... i have a vector of strings. i need to find a certain string's location within the contents of the vector, and return that location as an integer. i just want to know if there's any member functions to the vector class to do what i want, or am i forced to write a while loop to go look from 0 to vector.size() seeing if i can gert a match? (that's kinda boring :p)
 
I believe there is an algorithm (ie global function) called find that will do what you want, but I havent played with stl much (although I need too before my final) so im not sure if it would work.

You could always just write a function that uses iterators in the fashion you want, but im not sure if your goal is to take the search code from the main flow or just use something already provided.

I'll probably look it up for ya later, just no one has replied yet.
 
um, a vector of strings? You'll have to go through each string in the vector to find your substring, and you'll have to used find() on each string.

Walleye said:
alright... i'm looking for a more elegant way of doing this than what i have in mind currently.

okay.... i have a vector of strings. i need to find a certain string's location within the contents of the vector, and return that location as an integer. i just want to know if there's any member functions to the vector class to do what i want, or am i forced to write a while loop to go look from 0 to vector.size() seeing if i can gert a match? (that's kinda boring :p)
 
I believe that any kind of member function for vectors for finding values will return iterators or pointers to the location. Never an integer. I'm assuming by integer, you mean like an array subscript.
 
alright, i just did it the dirty way i described... wasnt that bad. i'll have more questions later :p
 
alright, i'm having a new problem.

i'm trying to turn a char array into strings of words that are in the char array.

i used the getline function to read a user inputted line of code... now i have to turn that line of code into a vector of strings.

rather embarrasingly, the only way i know how to do this is run it out to a temporary file, then read it into a vector from the file, then destroy the file...

i know there's gotta be a better way, but i'm having issues thinking of it.
 
Walleye said:
alright, i'm having a new problem.

i'm trying to turn a char array into strings of words that are in the char array.

i used the getline function to read a user inputted line of code... now i have to turn that line of code into a vector of strings.

rather embarrasingly, the only way i know how to do this is run it out to a temporary file, then read it into a vector from the file, then destroy the file...

i know there's gotta be a better way, but i'm having issues thinking of it.

Not sure I understand you... you have a char array that you want to break up into words to store in a string array?

If you used the getline function, you should be able to read the input directly into a string.

A sort of string tokenizer I wrote for some homework a few weeks ago... modified for your case.

Code:
    int i = 0;
    string wrd = "";
    vector<string> v;
    
    while(i < str.length())
    {
        if(isalpha(str[i]))  //iterate through string one character at a time
            wrd += str[i];  //if string[i] a letter, add it to the word;
        else if(wrd.length() > 0)  //if there is something in string word
            {
                v.insert(wrd);
                wrd = "";  //start over for word...
            }
        i++;
    }

Something like that.

EDIT:
Duh... the above example was if you were using C++ strings. If you're just using character arrays, you should be able to use strtok(), and then maybe convert the char to string by doing something like using strings .push_back(ch) and looping through the whole character array.
 
theDot said:
Not sure I understand you... you have a char array that you want to break up into words to store in a string array?

If you used the getline function, you should be able to read the input directly into a string.

A sort of string tokenizer I wrote for some homework a few weeks ago... modified for your case.

Code:
    int i = 0;
    string wrd = "";
    vector<string> v;
    
    while(i < str.length())
    {
        if(isalpha(str[i]))  //iterate through string one character at a time
            wrd += str[i];  //if string[i] a letter, add it to the word;
        else if(wrd.length() > 0)  //if there is something in string word
            {
                v.insert(wrd);
                wrd = "";  //start over for word...
            }
        i++;
    }

Something like that.

Or did you want the char array broken up into individual words?

ya. multiple words in an 80 character char array. i need to break em up by spaces into individual words, to store in a vector of strings.

(that way, i can flip em around and type em back out real easy... not very practical, but it's what the prompt asks for..)
 
if you are simply trying to have a vector or array with a bunch of strings in it, then whad you need to do is use two seperate parts. One of them will be the character array, or string class if you prefer. The substrings are stuffed into there back to back, with the \0 at the end and between each substring. The second part will be an array or vector that will be full of pointers pointing to the starts of the substrings within the first char array/string.

edit: guess thats not what you are doing

Walleye said:
alright, i'm having a new problem.

i'm trying to turn a char array into strings of words that are in the char array.

i used the getline function to read a user inputted line of code... now i have to turn that line of code into a vector of strings.

rather embarrasingly, the only way i know how to do this is run it out to a temporary file, then read it into a vector from the file, then destroy the file...

i know there's gotta be a better way, but i'm having issues thinking of it.
 
Use a string stream to break your string into tokens.

#include <sstream>

istringstream sstream( char *str );

sstream >> token1 >> token2 >> etc;
 
Back
Top