Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to store a string into a vector?

Reply
Thread Tools

how to store a string into a vector?

 
 
vijetha
Guest
Posts: n/a
 
      01-01-2006
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

 
Reply With Quote
 
 
 
 
Stephan Grein
Guest
Posts: n/a
 
      01-01-2006
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, <(E-Mail Removed)>
http://hagbard.ninth-art.de/
GnuPG-Key-ID: 0x08FA3507
<ESC> :wq
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      01-01-2006
In article <(E-Mail Removed) .com>,
"vijetha" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      01-02-2006
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

 
Reply With Quote
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      01-02-2006

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

 
Reply With Quote
 
James Juno
Guest
Posts: n/a
 
      01-02-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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:ointer if
he really wants to get to that buffer.

--JJ
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-02-2006

James Juno wrote:

> (E-Mail Removed) 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:ointer if
> he really wants to get to that buffer.


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

Gavin Deane

 
Reply With Quote
 
James Juno
Guest
Posts: n/a
 
      01-02-2006
Gavin Deane wrote:
> James Juno wrote:
>
>> (E-Mail Removed) 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:ointer if
>> he really wants to get to that buffer.

>
> string:ointer 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
 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      01-03-2006
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

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-03-2006

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

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Read HTML code into c# program, store in string amitM ASP .Net 2 09-22-2010 08:04 PM
Re: How to store picture (*.jpg, *.bmp, *.gif) into sql database ? Curt_C [MVP] ASP .Net 3 08-14-2009 01:33 AM
to store or not to store an image =?Utf-8?B?UnVkeQ==?= ASP .Net 6 03-30-2005 05:51 AM
How do you centrally store and include reusable javascript functions into .aspx pages? Nikki ASP .Net 3 07-02-2003 12:42 AM



Advertisments