By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

how to insert a string in vector c++

P: 1
vector <string> mystring;
mystring.push_back("Hello World");
but when i print after the insertion then i get ("hello") and ("World") as different string.
i need to get ("Hello World ") As one string..
pls help me..
Feb 25 '17 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
You are not pushing back a string. "Hello world" is a const char*.

With function overloading, there are many push_back methods and the one used is based on the argument you provide.

Try:

Expand|Select|Wrap|Line Numbers
  1. string obj("Hello World");
  2. mystring.push_back(obj);
The push_back with a const char* argument stops pushing when it gets a whitespace byte. Just like it should if this were C. So you push Hello and it stops.

What is called a string in C is not a string in C++. In C++ a string means a string object. In C++ a C-string is just a \0 terminated array of char.
Feb 25 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.