Reputation: 775
I'm trying to concatenate a string with a vector size. Whatever method I use, I don't get the desired output. When I use cout
it prints fine and when I look at the value of the string in my debugger, it appears as Schemes(\002)
. Problem is: I need to return a string, rather than print straight to the console, so I can't use cout
; I must use concatenation. Why is the string and the vector size not concatenating as expected?
Desired string: schemes(2)
Outputted string: schemes()
Code:
using namespace std;
string s;
vector<Object> schemes;
// Add two elements to vector
// Method 1 (doesn't work)
s += "Schemes(" + schemes.size();
s += ")"; // I can't put this on the same line because I get 'expression must have integral or unscoped enum type' error
// Method 2 (doesn't work)
s += "Schemes(";
s.push_back(schemes.size());
s += ")";
// Method 3 (doesn't work)
s += "Schemes(";
s.append(schemes.size());
s += ")";
Upvotes: 0
Views: 1074
Reputation: 854
schemes.size() is an integer type. This means you're trying to concatenate an integer type to a string type.
Try
s = "Schemes(" + to_string(schemes.size()) + ")";
Upvotes: 3