|
-
February 23rd, 2011, 05:17 AM
#1
passing an iterator to a function
Hello all
Is there anyway I can pass an iterator to a function?
Thanks
-
February 23rd, 2011, 05:24 AM
#2
Re: passing an iterator to a function
What's your problem? You can surely pass an iterator to a function.
-
February 23rd, 2011, 05:39 AM
#3
Re: passing an iterator to a function
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
February 23rd, 2011, 05:46 AM
#4
Re: passing an iterator to a function
Actually passing an iterator also wont do. I have vectors of different struct types. A "space separated" string is stored in one of the struct variables (all of type string) in each vector. I have to remove spaces from the "space separated" string and replace it with a comma to make it a csv string but except at one place i-e I want to keep that substring still a "space separated" string and I have also stored that "space separated" string separately in one of the struct elements.
e-g
Code:
element-1"space separated" string part of the string: "abc 123" which is for example the fifth/sixth substring in the example below
element-2 of vector-structtype-1: "shnd 23j l94 hsi8 abc 123 md38 fhw0 8fdj"
csv element-2 of vector-structtype-1: "shnd, 23j, l94, hsi8, abc 123, md38 fhw0 8fdj"
element-1 of vector-structtype-2: "space separated" string part of the string: "klm 987" which is for example the fifth/sixth substring in the example below
element-2 of vector-structtype-2: "dnhs j32 49l 8ish klm 987 83dm 0wfh jdf8"
csv element-2 of vector-structtype-2: "dnhs, j32, 49l, 8ish, klm 987, 83dm, 0wfh, jdf8"
The code below works fine but I dont know how to keep that specific substring still a "space separated" string.
Code:
void liffe_breach::CSVline(int Count, int ordIDno, string str) // I want this str to be an elements of a specific vector. Is it possible?
{ // because vectors are of different (struct) types
std::stringstream Brchno;
Brchno << Count;
std::stringstream IDno;
IDno << ordIDno;
stringstream ss(str);
string word;
string cvsLine = "";
while ( ss >> word )
{
cvsLine = cvsLine + word + ",";
}
}
-
February 23rd, 2011, 05:58 AM
#5
Re: passing an iterator to a function
You can temporarily replace the spaces you don't want to transform into another character (for example "_"), run your program, and then re-insert the space back in:
Code:
Step 0: "shnd 23j l94 hsi8 abc 123 md38 fhw0 8fdj"
Step 1: "shnd 23j l94 hsi8 abc_123 md38 fhw0 8fdj"
Step 2: "shnd, 23j, l94, hsi8, abc_123, md38, fhw0, 8fdj"
Step 3: "shnd, 23j, l94, hsi8, abc 123, md38 fhw0 8fdj"
EDIT: You know, you wouldn't be having all these problems if you were actually modeling your data, instead of always trying to do string operations.
EDIT2: perl (and regexes in general) is much better suited for this task. This is a 3-line perl script.
Last edited by monarch_dodra; February 23rd, 2011 at 06:03 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
February 27th, 2011, 10:30 AM
#6
Re: passing an iterator to a function
Is it always the same field that you want to keep the space ? If so,
you can keep a counter to determine whether to keep the space
or put a comma.
Code:
std::stringstream Brchno;
Brchno << Count;
std::stringstream IDno;
IDno << ordIDno;
stringstream ss(str);
string word;
string cvsLine;
int field = 0;
while ( ss >> word )
{
cvsLine = cvsLine + word;
if (field == 4)
cvsLine += " ";
else
cvsLine += ",";
++field;
}
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
|