473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Jan 1 '06 #1
9 34611
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, <St***********@gmail.com>
http://hagbard.ninth-art.de/
GnuPG-Key-ID: 0x08FA3507
<ESC> :wq
Jan 1 '06 #2
In article <11**********************@g49g2000cwa.googlegroups .com>,
"vijetha" <vi*****************@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.
Jan 1 '06 #3
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

Jan 2 '06 #4

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

Jan 2 '06 #5
Mi*************@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
Jan 2 '06 #6

James Juno wrote:
Mi*************@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

Jan 2 '06 #7
Gavin Deane wrote:
James Juno wrote:
Mi*************@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
Jan 2 '06 #8
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

Jan 3 '06 #9

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

Jan 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
26
by: Peter Mount | last post by:
Hello What's the syntax for using fgets() to store a string in memory? I understand that fgets() can solve the problem of storing a string that has more characters than the size of the declared...
1
by: Vaj | last post by:
Hi, I'm attaching a code here.this code works successfully, But my doubt is, Can an arraylist store this integer value "J" as wellas string value "S" ArrayList ArrMM=new ArrayList(); for(int...
2
by: John Wildes | last post by:
hello I was wondering if someone could point me in the direction of information on using app.config to store string variables. I have a couple of variables that store path information for file...
2
by: martin-g | last post by:
Hi. Almost every application have to write out some messages to the user. The question is how to store them. For example, while programming for Windows in C++ we could store these messages as...
3
by: kumarchain | last post by:
i want to know the detail of vector and where it implemented,and something in collections please reply and also iam want to know the popular site for learning jsp and stuts otherthan java site
9
by: shoes2908 | last post by:
Hi, again I am in a rush and can't seem to remember how to search for a certain string in a string vector... heres my code: cin >> worker_num; if (cin.fail()) {...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.