how to store a string into a vector?

V

vijetha

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
 
S

Stephan Grein

vijetha said:
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,
 
D

Daniel T.

"vijetha said:
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' );
}
 
L

Luke Meyers

Daniel said:
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
 
M

Michiel.Salters

Luke said:
Daniel said:
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
 
J

James Juno

Luke said:
Daniel said:
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
 
G

Gavin Deane

James said:
Luke said:
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
 
J

James Juno

Gavin said:
James said:
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
 
L

Luke Meyers

James said:
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
 
G

Gavin Deane

Luke said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top