|
-
July 8th, 2003, 02:23 AM
#1
put std string into array
Hello. I need your help on placing std strings into array.
I used the following in declaring array of string but none of them seem to work. I have 10 std strings and I want to put them in an array.
Code:
string mystringcollection[10]; //I don't know which of the following
char *mystringcollection[10]; //is the correct declaration
Also how do I go back, or read in a part in file after the while statement below. Because when I try to read in a line after the while statement, it doesn't read in anything ( I think it has already reached the end of file) but I need to get access to that part of the file.
Code:
while (getline(infile,line)) { //get next line of input file
//search for something in file and do something
}
//go back to next part of file
getline(infile, line); outfile<<line; //outputs nothing
Thanks in advance for your suggestion/help.
-
July 8th, 2003, 02:46 AM
#2
Why do you want to put your std::strings into an array? Why not use one of the STL container classes? Anyway, declaration number one will probably do the trick:
Code:
std::string ar[2];
ar[1] = "Hello";
-
July 8th, 2003, 10:37 PM
#3
Thanks sbrothy for your help. I decided to put my strings into a vector.
Anyway I got stuck in another problem in my code. I need to construct a loop that does something until program finds "loc" character. Here's the code.
Code:
getline(infile,line);
int i=0;
string::size_type locpos = line.find(loc[i]); //search for "loc" string in line
while () { //do this until find character "loc"
int i=0;
string::size_type calpos = line.find(cal[i]); /look for "cal" string in line
if(calpos != std::string::npos) {
string calext(".CAL");
string calfile = line.substr(0, calpos)+calext; //add .cal to string filename
calvec.push_back(calfile); //the part where I store strings inside vector
}
}
Last edited by ambc; July 8th, 2003 at 11:17 PM.
-
July 9th, 2003, 12:32 AM
#4
I figured it out. To those who may encounter the same problem
Code:
string::size_type locpos;
do {
//do something
}while (locpos == std::string::npos);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|