0
Can auto be considered as vector<int> or not
Hi Please refer code below: It currently fails to compile due to last line called for vector.. Is it like auto does not work for vector<int> or Am i missing something ? https://code.sololearn.com/cj7G8w80GitG/?ref=app
7 odpowiedzi
+ 1
It will only print Hi\n when you are printing the vector.
You have 3 test cases:
string s1 = "Abc";
string s2 = "Abc";
printMismatch(s1,s2); => It will give no mismatch between Abc and Abc
string s3 = "Abc";
string s4 = "Axc";
printMismatch(s3,s4); => It will give b x
vector<int> v1{1,2,3};
vector<int> v2{1,2,5};
printMismatch(v1,v2); => It will give 3 5
Note: On the 3rd case you are not printing the vector, instead you are printing the differences
Let's put another test case:
vector<int> v1{1,2,5};
vector<int> v2{1,2,5};
printMismatch(v1,v2); => It will give No mismatch between [ 1 2 3 ]Hi
and [1 2 3]Hi
+ 5
As @Tomás Ribiero said, you can't print vectors. On line 15, you are printing 's1' and 's2' which are 'vector<int>&'.
+ 1
If you read the error the compiler is complaining about the << operator on cout. The problem is you can't cout a vector. What you really want is to overload the << operator: https://stackoverflow.com/questions/4077609/overloading-output-stream-operator-for-vectort).
To make it simpler I implemented the overload: https://www.sololearn.com/compiler-playground/cBVgP5t40CEX
+ 1
Thanks Tomás Ribeiro ...
I had this doubt when checked error but how come it can be issue ?
I am not printing vector.... I am dererencing pair of two iterators
And as data is int for vector, cout should be fine to print it
Also in your code, I tried adding below line
os << " Hi\n";
in overloaded << but it's not printing...so clear that it's not required....am I missing something ?
+ 1
#include <iostream>
#include <algorithm>
using namespace std;
void printMismatch(auto& s1,auto& s2){
auto itr = mismatch(s1.begin(),s1.end(),s2.begin());
if (*itr.first != *s2.end())
cout << *itr.first << " " << *itr.second << endl;
else
cout << "No mismatch found \n";
}
int main(){
string s1 = "Abc";
string s2 = "Abc";
printMismatch(s1,s2);
string s3 = "Abc";
string s4 = "Axc";
printMismatch(s3,s4);
vector<int> v1{1,2,3};
vector<int> v2{1,2,5};
printMismatch(v1,v2);
}
0
Thanks Tomás Ribeiro and XXX
0
Tomás Ribeiro
overloading the << feels like overkill.😁
just simplify the no mismatch else statement...
Ketan Lalcheta
you are using cout on the vectors in your "no mismatch found" else statement.