Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > char array nummterminated to std::vector<std::string>

Reply
Thread Tools

char array nummterminated to std::vector<std::string>

 
 
Philipp Kraus
Guest
Posts: n/a
 
      04-21-2011
Hello,

I get a char array (out of another component) like this
abcd*xyz*mnop*

* is a seperator nullterm char. How can I seperate the char array at
the * and push back them to a std;;vector<std::string> ?

Thanks

Phil

 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      04-21-2011

"Philipp Kraus" <(E-Mail Removed)> wrote in message
news:iopscs$nj7$(E-Mail Removed)...
> Hello,
>
> I get a char array (out of another component) like this
> abcd*xyz*mnop*
>
> * is a seperator nullterm char. How can I seperate the char array at the *
> and push back them to a std;;vector<std::string> ?
>
> Thanks
>


There is a C function called strtok() that does this.

http://www.cplusplus.com/reference/c...string/strtok/

 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      04-21-2011

"Philipp Kraus" <(E-Mail Removed)> wrote in message
news:iopscs$nj7$(E-Mail Removed)...
> Hello,
>
> I get a char array (out of another component) like this
> abcd*xyz*mnop*
>
> * is a seperator nullterm char. How can I seperate the char array at the *
> and push back them to a std;;vector<std::string> ?
>
> Thanks
>

There is another way explained on this page at section 7.3 , you may prefer.
http://www.oopweb.com/CPP/Documents/...g-HOWTO-7.html

 
Reply With Quote
 
Virchanza
Guest
Posts: n/a
 
      04-21-2011
On Apr 21, 7:18*pm, Philipp Kraus <(E-Mail Removed)> wrote:
> Hello,
>
> I get a char array (out of another component) like this
> abcd*xyz*mnop*
>
> * is a seperator nullterm char. How can I seperate the char array at
> the * and push back them to a std;;vector<std::string> ?


vector<string> MakeVecString(char *p, char const z)
{
assert( p != 0 );
assert( *p != '\0');
assert( z != '\0');

vector<string> vecstr;

for (;
{
char *const q = strchr(p, z);

if ( q )
{
*q = '\0';
}

vecstr.push_back(p);

if ( !q )
break;

p = q + 1;
}

return vecstr;
}
 
Reply With Quote
 
Philipp Kraus
Guest
Posts: n/a
 
      04-21-2011
On 2011-04-21 20:59:35 +0200, Virchanza said:

> On Apr 21, 7:18*pm, Philipp Kraus <(E-Mail Removed)> wrote:
>> Hello,
>>
>> I get a char array (out of another component) like this
>> abcd*xyz*mnop*
>>
>> * is a seperator nullterm char. How can I seperate the char array at
>> the * and push back them to a std;;vector<std::string> ?

>
> vector<string> MakeVecString(char *p, char const z)
> {
> assert( p != 0 );
> assert( *p != '\0');
> assert( z != '\0');
>
> vector<string> vecstr;
>
> for (;
> {
> char *const q = strchr(p, z);
>
> if ( q )
> {
> *q = '\0';
> }
>
> vecstr.push_back(p);
>
> if ( !q )
> break;
>
> p = q + 1;
> }
>
> return vecstr;
> }


This seperates the chars. On my example I need a vector in this case:
vec[0] = abcd
vec[1] = xyz
vec[2] = mnop

Thanks

 
Reply With Quote
 
Philipp Kraus
Guest
Posts: n/a
 
      04-21-2011
On 2011-04-21 20:28:35 +0200, Paul said:

> "Philipp Kraus" <(E-Mail Removed)> wrote in message
> news:iopscs$nj7$(E-Mail Removed)...
>> Hello,
>>
>> I get a char array (out of another component) like this
>> abcd*xyz*mnop*
>>
>> * is a seperator nullterm char. How can I seperate the char array at
>> the * and push back them to a std;;vector<std::string> ?
>>
>> Thanks
>>

>
> There is a C function called strtok() that does this.
>
> http://www.cplusplus.com/reference/c...string/strtok/


I have tried this with the delimiter \0 but it does not work. I will
get only the first part "abcd". On my example above I would like to get
a result vector in this way:
vec[0] = "abcd"
vec[1] = "xyz"
vec[2] = "mnop"


Thanks for help

Phil

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      04-21-2011
Philipp Kraus wrote:

> Hello,
>
> I get a char array (out of another component) like this
> abcd*xyz*mnop*
>
> * is a seperator nullterm char. How can I seperate the char array at
> the * and push back them to a std;;vector<std::string> ?


Well, the idea would be to search for the first * and then construct the
first string from the initial segment. Then, you would search for the second
* and construct the second string from the segment between the two *. Now,
you go on.

The problem I see is this: how do you tell when to stop; i.e., how can you
know detect that a * that you find is the last. It appears that this is not
encoded in your input data. At least, it seems, you have left out that part
in your description.


Best,

Kai-Uwe Bux

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      04-21-2011

"Philipp Kraus" <(E-Mail Removed)> wrote in message
news:ioq1lt$sba$(E-Mail Removed)...
> On 2011-04-21 20:28:35 +0200, Paul said:
>
>> "Philipp Kraus" <(E-Mail Removed)> wrote in message
>> news:iopscs$nj7$(E-Mail Removed)...
>>> Hello,
>>>
>>> I get a char array (out of another component) like this
>>> abcd*xyz*mnop*
>>>
>>> * is a seperator nullterm char. How can I seperate the char array at the
>>> * and push back them to a std;;vector<std::string> ?
>>>
>>> Thanks
>>>

>>
>> There is a C function called strtok() that does this.
>>
>> http://www.cplusplus.com/reference/c...string/strtok/

>
> I have tried this with the delimiter \0 but it does not work. I will get
> only the first part "abcd". On my example above I would like to get a
> result vector in this way:
> vec[0] = "abcd"
> vec[1] = "xyz"
> vec[2] = "mnop"
>
>
> Thanks for help
>

You could just do something like this:

#include <iostream>
#include <vector>
#include <string>

void splitstring(std::vector<std::string>& v, char* p, char c){
std::string str;
while (*p != '\0'){
if (*p == c){
v.push_back(str);
str.clear();
++p;
}
str+= *p;
++p;
}
if(*(str.data()) != '\0')
v.push_back(str);
}

void printvector(std::vector<std::string> v){
for (int i=0; i<v.size() ; ++i ){
std::cout<< v[i] << std::endl;
}
}

int main(){
std::vector<std::string> v1;
std::vector<std::string> v2;
char arr1[] = "abcd*xyz*mnop*";
char arr2[] = "abcd*xyz*mnop*sgegsgsgga";

splitstring(v1, arr1, '*');
splitstring(v2, arr2, '*');
std::cout<<"Printing vector v1:\n";
printvector(v1);
std::cout<<"Printing vector v2:\n";
printvector(v2);

}


The above is not perfect because if your first char is a '*' it would create
an empty string at v[0], but it may be something you can work on.

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      04-21-2011

"Leigh Johnston" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 21/04/2011 21:23, Paul wrote:
>>>>> Hello,
>>>>>
>>>>> I get a char array (out of another component) like this
>>>>> abcd*xyz*mnop*
>>>>>
>>>>> * is a seperator nullterm char. How can I seperate the char array at
>>>>> the * and push back them to a std;;vector<std::string> ?
>>>>>
>>>>> Thanks
>>>>>
>>>>
>>>> There is a C function called strtok() that does this.
>>>>
>>>> http://www.cplusplus.com/reference/c...string/strtok/
>>>
>>> I have tried this with the delimiter \0 but it does not work. I will
>>> get only the first part "abcd". On my example above I would like to
>>> get a result vector in this way:
>>> vec[0] = "abcd"
>>> vec[1] = "xyz"
>>> vec[2] = "mnop"
>>>
>>>
>>> Thanks for help
>>>

>> You could just do something like this:
>>
>> #include <iostream>
>> #include <vector>
>> #include <string>
>>
>> void splitstring(std::vector<std::string>& v, char* p, char c){
>> std::string str;
>> while (*p != '\0'){

>
> You need to learn to name variables more intelligently; 'p' for 'pointer'
> and 'str' for 'string' is all very n00bish; names should be chosen based
> on *role* not *type*.
>

The naming convention god has spoken. Now we all must use Leighs naming
conventions.
Would you like CamelCase or underscores?

 
Reply With Quote
 
Philipp Kraus
Guest
Posts: n/a
 
      04-21-2011
On 2011-04-21 22:15:07 +0200, Kai-Uwe Bux said:

> Philipp Kraus wrote:
>
>> Hello,
>>
>> I get a char array (out of another component) like this
>> abcd*xyz*mnop*
>>
>> * is a seperator nullterm char. How can I seperate the char array at
>> the * and push back them to a std;;vector<std::string> ?

>
> Well, the idea would be to search for the first * and then construct the
> first string from the initial segment. Then, you would search for the second
> * and construct the second string from the segment between the two *. Now,
> you go on.


Is there a C or C++ function which I can use for seperating the parts?
Or should I iterate over all elements, if I read a \0 char, I push back
a element to the vector and
than create a new string element on which I append the readed char.

> The problem I see is this: how do you tell when to stop; i.e., how can you
> know detect that a * that you find is the last. It appears that this is not
> encoded in your input data. At least, it seems, you have left out that part
> in your description.


I know the length of the char array, so I can stop if I reach the end.

Phil

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Aw: char array nummterminated to std::vector<std::string> Syron C++ 1 04-23-2011 10:07 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments