I am trying to create a vector of integers and then write it out through a function that would print the values with a certain width.
Say the vector
was filled with {1,2,3,4}
and the limiting width is 4
, then the output is: 1 2 2 4
Using turntostring
, which converts data type into string and return int, I was able to turn int into string and get its size in my function that prints the vector.
turntostring(r.at(i)).size();
So at i = 2
, I have int = 4
, with the code above the return value is 1
.
It was fine until I have to do the same for a vector of classes and as I am trying to access the element in this class I am unable to use the same line of code above.
I tried to write a print function for the class and write this in order to obtain the same result:
r.at(i).print().size();
Print would take the class and return a string.
But I need to use the same line of code to access the size of the element as a requirement. Meaning the same line of code must work for a vector
of type int and a vector
of class with a member of type int for example:
class fake
{
int num;
}
vector<int> r;
vector<fake> k;
I should be able to access each element in these two vectors and use .size()
then use turnintostring
on them.
Please let me know if there is some method that I do not know about.
If you already have string turntostring(int)
then you just need a cast:
class fake
{
int num;
public:
operator int();
}
fake::operator int() {return num;}
turntostring(some_fake);
turntostring(3);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.