Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   how to store a string into a vector? (http://www.velocityreviews.com/forums/t450819-how-to-store-a-string-into-a-vector.html)

vijetha 01-01-2006 10:37 PM

how to store a string into a vector?
 
Hi,
I have been trying to store a string into a vector but I am not
able to figure out how to do it. I am able to use vectors for storing
integers but not able to use them fro strings. I want to be able to
convert vector to string and vice versa.
Cheers,
Vijetha


Stephan Grein 01-01-2006 10:42 PM

Re: how to store a string into a vector?
 
vijetha wrote:
> Hi,
> I have been trying to store a string into a vector but I am not
> able to figure out how to do it. I am able to use vectors for storing
> integers but not able to use them fro strings. I want to be able to
> convert vector to string and vice versa.
> Cheers,
> Vijetha
>

Guessing about what you mean:

#include <string>
#include <vector>

int main(void) {
std::vector<std::string> vestring;
vestring.push_back("A String");
}

Greetings,
--
Stephan 'hagbard' Grein, <Stephan.Grein@gmail.com>
http://hagbard.ninth-art.de/
GnuPG-Key-ID: 0x08FA3507
<ESC> :wq

Daniel T. 01-01-2006 11:03 PM

Re: how to store a string into a vector?
 
In article <1136155043.102489.227080@g49g2000cwa.googlegroups .com>,
"vijetha" <vijetha.vemulapalli@gmail.com> wrote:

> Hi,
> I have been trying to store a string into a vector but I am not
> able to figure out how to do it. I am able to use vectors for storing
> integers but not able to use them fro strings. I want to be able to
> convert vector to string and vice versa.
> Cheers,
> Vijetha


Do you mean something like this?

#include <string>
#include <vector>

int main() {
using namespace std;
string s( "hello world" );

vector<char> v( s.begin(), s.end() );

assert( v.size() == 11 );
assert( v[0] == 'h' );
assert( v[10] == 'd' );
}


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.

Luke Meyers 01-02-2006 03:50 AM

Re: how to store a string into a vector?
 
Daniel T. wrote:
> Do you mean something like this?
>
> #include <string>
> #include <vector>
>
> int main() {
> using namespace std;
> string s( "hello world" );
>
> vector<char> v( s.begin(), s.end() );
>
> assert( v.size() == 11 );
> assert( v[0] == 'h' );
> assert( v[10] == 'd' );
> }


I certainly hope not... why would someone want to do this?

Luke


Michiel.Salters@tomtom.com 01-02-2006 11:41 AM

Re: how to store a string into a vector?
 

Luke Meyers wrote:
> Daniel T. wrote:
> > Do you mean something like this?
> >
> > #include <string>
> > #include <vector>
> >
> > int main() {
> > using namespace std;
> > string s( "hello world" );
> >
> > vector<char> v( s.begin(), s.end() );
> >
> > assert( v.size() == 11 );
> > assert( v[0] == 'h' );
> > assert( v[10] == 'd' );
> > }

>
> I certainly hope not... why would someone want to do this?


Because he has an API doUpperCase(char*), but s.c_str() returns only a
char const*? &v[0] is a proper char*.

HTH,
Michiel Salters


James Juno 01-02-2006 05:28 PM

Re: how to store a string into a vector?
 
Michiel.Salters@tomtom.com wrote:
> Luke Meyers wrote:
>> Daniel T. wrote:
>>> Do you mean something like this?
>>>
>>> #include <string>
>>> #include <vector>
>>>
>>> int main() {
>>> using namespace std;
>>> string s( "hello world" );
>>>
>>> vector<char> v( s.begin(), s.end() );
>>>
>>> assert( v.size() == 11 );
>>> assert( v[0] == 'h' );
>>> assert( v[10] == 'd' );
>>> }

>> I certainly hope not... why would someone want to do this?

>
> Because he has an API doUpperCase(char*), but s.c_str() returns only a
> char const*? &v[0] is a proper char*.
>
> HTH,
> Michiel Salters
>


This is still an abomination. He's better off using string::pointer if
he really wants to get to that buffer.

--JJ

Gavin Deane 01-02-2006 06:56 PM

Re: how to store a string into a vector?
 

James Juno wrote:

> Michiel.Salters@tomtom.com wrote:
> > Luke Meyers wrote:
> >> Daniel T. wrote:
> >>> Do you mean something like this?
> >>>
> >>> #include <string>
> >>> #include <vector>
> >>>
> >>> int main() {
> >>> using namespace std;
> >>> string s( "hello world" );
> >>>
> >>> vector<char> v( s.begin(), s.end() );
> >>>
> >>> assert( v.size() == 11 );
> >>> assert( v[0] == 'h' );
> >>> assert( v[10] == 'd' );
> >>> }
> >> I certainly hope not... why would someone want to do this?

> >
> > Because he has an API doUpperCase(char*), but s.c_str() returns only a
> > char const*? &v[0] is a proper char*.
> >
> > HTH,
> > Michiel Salters
> >

>
> This is still an abomination. He's better off using string::pointer if
> he really wants to get to that buffer.


string::pointer is a typedef. How does that help?

Gavin Deane


James Juno 01-02-2006 07:23 PM

Re: how to store a string into a vector?
 
Gavin Deane wrote:
> James Juno wrote:
>
>> Michiel.Salters@tomtom.com wrote:
>>> Luke Meyers wrote:
>>>> Daniel T. wrote:
>>>>> Do you mean something like this?
>>>>>
>>>>> #include <string>
>>>>> #include <vector>
>>>>>
>>>>> int main() {
>>>>> using namespace std;
>>>>> string s( "hello world" );
>>>>>
>>>>> vector<char> v( s.begin(), s.end() );
>>>>>
>>>>> assert( v.size() == 11 );
>>>>> assert( v[0] == 'h' );
>>>>> assert( v[10] == 'd' );
>>>>> }
>>>> I certainly hope not... why would someone want to do this?
>>> Because he has an API doUpperCase(char*), but s.c_str() returns only a
>>> char const*? &v[0] is a proper char*.
>>>
>>> HTH,
>>> Michiel Salters
>>>

>> This is still an abomination. He's better off using string::pointer if
>> he really wants to get to that buffer.

>
> string::pointer is a typedef. How does that help?
>
> Gavin Deane
>


Point taken, but the whole thing is ugly from a readability stand-point
and in this case, I hope whatever location he passes to the API function
doesn't affect the length of the array. Granted, my solution doesn't
help in that case either. Thankfully we can do something like:

transform(str.begin(), str.end(), str.begin(), toupper);

or some other such function-based manipulation.

-JJ

Luke Meyers 01-03-2006 05:50 AM

Re: how to store a string into a vector?
 
James Juno wrote:
> transform(str.begin(), str.end(), str.begin(), toupper);


Now you're cookin' with gas.

Keep in mind that toupper is in the global namespace, though, so you'll
have to either use qualifiers or using-decls for the std stuff, or
qualify it as ::toupper. The following compiles and works:

#include <string>
#include <algorithm>
#include <cctype>
#include <iostream>

int main() {
using namespace std;
string s1 = "hello, world!";

string::iterator begin = s1.begin();
string::iterator end = s1.end();
transform(begin, end, begin, ::toupper);

cout << s1 << endl;

return EXIT_SUCCESS;
}

Luke


Gavin Deane 01-03-2006 08:10 AM

Re: how to store a string into a vector?
 

Luke Meyers wrote:

> James Juno wrote:
> > transform(str.begin(), str.end(), str.begin(), toupper);

>
> Now you're cookin' with gas.
>
> Keep in mind that toupper is in the global namespace, though, so you'll
> have to either use qualifiers or using-decls for the std stuff, or
> qualify it as ::toupper. The following compiles and works:


>From 17.4.1.2/4


[...] the contents of each header cname shall be the same as that of
the corresponding header name.h [...]. In the C++ Standard Library,
however, the declarations and definitions (except for names which are
defined as macros in C) are within namespace scope of the namespace
std.

> #include <string>
> #include <algorithm>
> #include <cctype>
> #include <iostream>
>
> int main() {
> using namespace std;
> string s1 = "hello, world!";
>
> string::iterator begin = s1.begin();
> string::iterator end = s1.end();
> transform(begin, end, begin, ::toupper);
>
> cout << s1 << endl;
>
> return EXIT_SUCCESS;
> }


So <cctype> puts toupper in the std namespace only. The above code is
therefore incorrect. The fact that it compiles on almost every compiler
out there is enough for me to prefer <name.h> to <cname> headers.
Deprecated maybe, but it's correct.

Gavin Deane



All times are GMT. The time now is 10:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.