Trouble building a dynamic string array. Coming from C#
-
I've already written my code in C# and it works. But to integrate it into our product, I have to re-write it C++ 6.0. I'm very weak here and am lost. My problem is this: I start with a line extracted from a file. The string contains a variable number of individual commands consisting of one letter followed by a number. In C# my code is built like this:
private string[] LineSplitter(string LineIn){
if (LineIn == "")
return null;
string[] strOut = new string[0];
int i = 0;
string strChar;
do
{
...code to locate length of number following letter
} while ((isNumeric(strChar)) && (i + 1 < LineIn.Length));
strOut = (string[])ResizeArray(strOut, strOut.Length + 1);
strOut[strOut.Length - 1] = LineIn.Substring(0, i);
LineIn = LineIn.Substring(i);
} while (LineIn.Length > 0);
return strOut;
}(And I'm sorry for putting C# in a C++ group, but I'm looking for the C++ equivalent) Here are my questions... 1. How do I declare the function as string array? 2. How do I increment array elements until I'm done? Each line can have a variable number of elements. 3. How do I return the result. Please be gentle. I'm struggling with C++ and this is my first post. I'm wrestling through the majority of everything else in this process, but this has my brain in a pickle.
-
I've already written my code in C# and it works. But to integrate it into our product, I have to re-write it C++ 6.0. I'm very weak here and am lost. My problem is this: I start with a line extracted from a file. The string contains a variable number of individual commands consisting of one letter followed by a number. In C# my code is built like this:
private string[] LineSplitter(string LineIn){
if (LineIn == "")
return null;
string[] strOut = new string[0];
int i = 0;
string strChar;
do
{
...code to locate length of number following letter
} while ((isNumeric(strChar)) && (i + 1 < LineIn.Length));
strOut = (string[])ResizeArray(strOut, strOut.Length + 1);
strOut[strOut.Length - 1] = LineIn.Substring(0, i);
LineIn = LineIn.Substring(i);
} while (LineIn.Length > 0);
return strOut;
}(And I'm sorry for putting C# in a C++ group, but I'm looking for the C++ equivalent) Here are my questions... 1. How do I declare the function as string array? 2. How do I increment array elements until I'm done? Each line can have a variable number of elements. 3. How do I return the result. Please be gentle. I'm struggling with C++ and this is my first post. I'm wrestling through the majority of everything else in this process, but this has my brain in a pickle.
Mark Randel wrote:
1. How do I declare the function as string array?
Not exactly an array but this is the C++ way -
std::vectorstd::string
Mark Randel wrote:
How do I increment array elements until I'm done? Each line can have a variable number of elements.
while (LineIn.size() > 0)
Mark Randel wrote:
How do I return the result.
std::vectorstd::string strOut;
.
.
.
return strOut;«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)modified on Wednesday, November 18, 2009 5:53 PM
-
Mark Randel wrote:
1. How do I declare the function as string array?
Not exactly an array but this is the C++ way -
std::vectorstd::string
Mark Randel wrote:
How do I increment array elements until I'm done? Each line can have a variable number of elements.
while (LineIn.size() > 0)
Mark Randel wrote:
How do I return the result.
std::vectorstd::string strOut;
.
.
.
return strOut;«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)modified on Wednesday, November 18, 2009 5:53 PM
Thanks a lot. The
vector
was the key to what I was looking for. I'm usingCString
, though. But it all works fine. My question about adding to the array was concerning changing the size of the array. The vector makes that easy with thepush_back
command. Also, returning the value from the function required that I make the type of the return as a vector:std::vector CNCLine::LineSplitter(CString LineIn)
{
std::vector strOut;
... code
return strOut;
}Works like clockwork. Thanks for the direction.
____________________________________________________ I'd rather have a frontal lobotomy than a bottle in front of me... Bill W
-
Thanks a lot. The
vector
was the key to what I was looking for. I'm usingCString
, though. But it all works fine. My question about adding to the array was concerning changing the size of the array. The vector makes that easy with thepush_back
command. Also, returning the value from the function required that I make the type of the return as a vector:std::vector CNCLine::LineSplitter(CString LineIn)
{
std::vector strOut;
... code
return strOut;
}Works like clockwork. Thanks for the direction.
____________________________________________________ I'd rather have a frontal lobotomy than a bottle in front of me... Bill W
Interesting... the post erased the "" command after the vector designation. Hope it doesn't with this text. Otherwise, I'm going to end up looking quite foolish. :D
____________________________________________________ I'd rather have a frontal lobotomy than a bottle in front of me... Bill W
-
Interesting... the post erased the "" command after the vector designation. Hope it doesn't with this text. Otherwise, I'm going to end up looking quite foolish. :D
____________________________________________________ I'd rather have a frontal lobotomy than a bottle in front of me... Bill W
That is because
<
and>
are using byHTML
for the tags. Replace<
with<
and>
with>
In fact you can simply click on the signs at the top of the editor to insert them.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)