473,395 Members | 1,379 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,395 software developers and data experts.

Can't cout vector<string>

Hi,

I'm trying to create a vector of strings and print the contents using
an iterator but I'm getting
an error with my very first string.

Can anyone help?

#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

int main()
{

char* temp1 = NULL;
vector<stringvec;
vector<string>::iterator it1;
temp1 = new char[21];
strcpy(temp1, "Test String");

vec.push_back(temp1);

for(it1 = vec.begin(); it1 != vec.end(); it1++)
{
cout << "Test: " << *it1 << endl;
}
}

Oct 19 '07 #1
5 15188
"Peithon" <Pe*****@googlemail.comwrote in message
news:11**********************@v29g2000prd.googlegr oups.com...
Hi,

I'm trying to create a vector of strings and print the contents using
an iterator but I'm getting
an error with my very first string.

Can anyone help?

#include <iostream>
#include <cstring>
<cstringis C's string header. strcpy, strcmp, etc... It does not include
C++'s std::string. Add the line:
#include <string>

and it should compile.
#include <vector>

using namespace std;

int main()
{

char* temp1 = NULL;
vector<stringvec;
vector<string>::iterator it1;
temp1 = new char[21];
strcpy(temp1, "Test String");

vec.push_back(temp1);

for(it1 = vec.begin(); it1 != vec.end(); it1++)
{
cout << "Test: " << *it1 << endl;
}
}

Oct 19 '07 #2
Peithon wrote:
Hi,

I'm trying to create a vector of strings and print the contents using
an iterator but I'm getting
an error with my very first string.

Can anyone help?
It looks ok. What error do you get?

Regards,

Zeppe
Oct 19 '07 #3
Peithon wrote:
Hi,

I'm trying to create a vector of strings and print the contents using
an iterator but I'm getting
an error with my very first string.

Can anyone help?

#include <iostream>
#include <cstring>
#include <vector>
ah ok, you didn't #include<string>.

Regards,

Zeppe
Oct 19 '07 #4
On 19 Oct, 09:40, Zeppe <ze...@remove.all.this.long.comment.yahoo.it>
wrote:
Peithon wrote:
Hi,
I'm trying to create a vector of strings and print the contents using
an iterator but I'm getting
an error with my very first string.
Can anyone help?
#include <iostream>
#include <cstring>
#include <vector>

ah ok, you didn't #include<string>.

Regards,

Zeppe
Thx very much. Fixed.

Oct 19 '07 #5
Peithon <Pe*****@googlemail.comwrote in news:1192781412.628377.255730
@v29g2000prd.googlegroups.com:
Hi,

I'm trying to create a vector of strings and print the contents using
an iterator but I'm getting
an error with my very first string.
It would help if you told us what the error is....
#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

int main()
{
char* temp1 = NULL;

vector<stringvec;
vector<string>::iterator it1;

temp1 = new char[21];
strcpy(temp1, "Test String");
Why bother with playing with dynamic memory?
vec.push_back(temp1);
Why not just:

vec.push_back("Test String");

for(it1 = vec.begin(); it1 != vec.end(); it1++)
{
cout << "Test: " << *it1 << endl;
}
}
Oct 19 '07 #6

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

Similar topics

12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
10
by: dalbosco | last post by:
Hello, I am new to STL and I've written the following code which crash. Could anyone tell me why this code crash? Thanks by advance. -- J-F #include <iostream>
4
by: misirion | last post by:
Ciao, vorrei poter scrivere e leggere su files binari dei vettori di stringhe, ed avrei implementato questo codice: ifstream fin(conf.c_str(),ios::binary); char inc; fin.read( inc,...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
5
by: Martin Jørgensen | last post by:
Hi, The piece of code I'm struggling with is so simple, that I hope nobody wants a complete example for answering the question: -------- string color_line; int data_type = 0; for(...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
2
by: Rockair | last post by:
hi! there is a class: class card { static vector<string> names; //... };
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",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.