Printf with Vectors.

Simple questions:

I'm trying to use printf to print an item in a vector<string>. What do I use?

I assumed it'd be along the lines of printf("%s \n", vector[0]); but this causes it to crash. printf("%s \n", &vector[0]); prints among other gibberish, love hearts which while heart warming isn't all that useful.

So what am I missing?
%s wants a C-string (char*). You can use the .c_str() method of strings to get a c-string out of it. However, why exactly do you need to use printf() when you can just use cout? You are already using C++.
Thanks for the response.

To answer your qestuion it's a point of curiosity as well as it looking more compact/neater.

For example:

cout << "Incorrect number of parameters for clause \"" + vectorToCheck[0].substr(0, vectorToCheck[0].find_first_of(",")) +"\" on line: " + lineToCheck.substr(0, lineToCheck.find_first_of(",")) << endl;

to me seems less readable than:

printf("Incorrect number of parameters for clause \"%s\" on line: %s.", vectorToCheck[0].substr(0, vectorToCheck[0].find_first_of(",")), lineToCheck.substr(0, lineToCheck.find_first_of(",")) );

Is there any reason not to use printf?
Oh also, I want to pass integers into it in places, and with cout I need stringstreams to do that don't I?
Is there any reason not to use printf?

printf is not extensible with user-defined types.

It crashes if you do anything wrong instead of giving compile errors.

It can be used easily wrong and hence is the source for a lot of printf-exploits (Exploits are bugs that can be used to break into computer systems).

It increases code bloat. Also, even one printf in your code make you link against a lot of code, as the compiler can't know which part of the big printf-implementation you are using. (This is not really interesting for C++. iostream is not that small either. Even if you only link against what you use, often it tend to be larger than the whole C-printf implementation in the end. But even in C, a lot of people shun "printf").


Ciao, Imi.
Oh also, I want to pass integers into it in places, and with cout I need stringstreams to do that don't I?


Nope.

1
2
int i = 23;
std::cout << "The number is " << i << std::endl;


Ciao, Imi.
Hrm, interesting stuff, thanks. So you'd say that the benefits don't outweight the cons?

The function was drawn to my attention looking at code my boss had done funnily enough!
I tried that, or more specifically:

cout << "#STAT reports the wrong amount of source lines, there are actually: " + noOfSourceLines << endl;

With noOfSourceLines being an integer and it prints:

"reports the wrong amount of source lines, there are actually: "

Hence changing it to...
stringstream ss;
ss.str("");
ss << noOfSourceLines;
cout << "#STAT reports the wrong amount of source lines, there are actually: " + ss.str() << endl;

EDIT: The ss.str("") may appear superfluous here but it's to clear it - ss is defined elsewhere and used multiple times in the actual code.
Last edited on
Ah ha, I see the difference. << not +. Gotcha.

Silly me.

Much appreciate the speedy responses guys.
Last edited on
Topic archived. No new replies allowed.