473,800 Members | 2,613 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 34656
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_b ack("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************ **********@g49g 2000cwa.googleg roups.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(cha r*), 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(cha r*), 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(cha r*), 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(cha r*), 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.b egin(), str.end(), str.begin(), toupper);

or some other such function-based manipulation.

-JJ
Jan 2 '06 #8
James Juno wrote:
transform(str.b egin(), 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::iterato r begin = s1.begin();
string::iterato r 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.b egin(), 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::iterato r begin = s1.begin();
string::iterato r 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
10630
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 namespace std; : : vector<string>* getTyphoon()
6
8145
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 character array, but I'd really like to read it into a vector of strings. Here's how I'm doing the read into the char array: int main() { string str1("booger"); string str2("test");
26
3287
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 array. Thanks Peter Mount info@petermount.au.com
1
3339
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 j=1;j<=12;j++) {if(j<10)
2
23647
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 creation and output, and i would like to store the paths in the app.config and have them read into variables that are declared as strings. I have only been able to use the app.config to store information that is associated with dynamic...
2
2449
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 string resource and load them using LoadString API function. I'm quite new to C#, and the best thing I've managed is creating private constant members of a class. E. g.: public class ExpandManager
3
1731
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
2110
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()) { cin >> name_mod; for ( short q = 0; q < list_names.size(); q++) { if ( list_names = name_mod) //here is where i am comparing ...
6
7387
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", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10507
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.