Can't cout vector<string>

P

Peithon

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<string> vec;
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;
}


}
 
J

Jim Langston

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

<cstring> is 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<string> vec;
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;
}


}
 
Z

Zeppe

Peithon said:
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
 
Z

Zeppe

Peithon said:
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
 
A

Andre Kostur

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<string> vec;
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");
 

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,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top