The function returns a vector of integers, but
cout
isn't directly able to print them. There are (at least) two alternative solutions:
- Iteratively print the vector items in the main function:
int main()
{
Solution twotwo;
vector<int> number = {2, 7, 11, 15};
int tg = 9;
vector<int> result = twotwo.twoSum(number, tg);
cout << "[";
for (size_t n = 0; n < result.size(); ++n)
{
cout << result[n];
if ( n != result.size()-1)
cout << ",";
}
cout << "]" << endl;
}
or..
- since it is a wonderful occasion for overloading the insertion operator
<<
, do exactly that:
ostream & operator << (ostream & os, const vector <int > & v)
{
os << "[";
for ( size_t n = 0; n < v.size(); ++n)
{
os << v[n];
if ( n != (v.size() - 1) )
os << ',';
}
os << "]";
return os;
}
int main()
{
Solution twotwo;
vector<int> number = {2, 7, 11, 15};
int tg = 9;
cout << twotwo.twoSum(number, tg) << endl;
}