in this 1st code t[0] return 'char' so the compiler throws an error illegal conversion from 'char' to 'const char*' :
Code:
//CODE 1
#include<iostream>
#include<vector>
#include<cctype>
#include<string>
using namespace std;
int main()
{
string t="hello";
/* If I had used 'toupper([0])' for direct initialization of string 's', it would have thrown error -> illegal conversion from int to const char* */
string s(t[0]);
return 0;
}
but in the second code I have used a vector of string type which I have changed from lower case to upper:
Code:
//CODE 2
#include<iostream>
#include<vector>
#include<cctype>
#include<string>
using namespace std;
int main()
{
vector<string> vec(1,"hello");
for(vector<string>::size_type j=0;j<vec[0].size();j++)
{
vec[0][j]=toupper(vec[0][j]);
}
cout<<vec[0]<<endl;
return 0;
}
this 2nd code is accepting 'int' returned by toupper and vector of string type is accepting it without any error why this dual behavior?
Or is it due to the int-to-char natural/automatic conversion is taking place due to vec[0][j] indicating a 'char'?