please help a c++ newbie understand what is going wrong here. I got compile error message of Non-constant-expression cannot be narrowed from type 'unsigned long' to 'int' in initializer list
on leetcode web and my local ubuntu terminal, but it works perfectly fine on my CLion IDE. Also could explain why I got the error and how to solve it?
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<vector<int>> heights({{1, 2, 2, 3, 5}, {3, 2, 3, 4, 4}, {2, 4, 5, 3, 1}, {6, 7, 1, 4, 5}, {5, 1, 1, 2, 4}});
deque<vector<int>> dq;
for (int i=0;i<heights.size();i++){
dq.push_back({i,heights[0].size()-1});
}
for (auto vec: dq){
if (vec.empty())
cout<< "vec is empty";
else
cout<<vec[0]<< " "<< vec[1]<<endl;
}
return 0;
}
Soon as switch to pair
from vector<int>
, the error is gone. It's quite confusing to a newbie like me. Please shed some light on this.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<vector<int>> heights({{1, 2, 2, 3, 5}, {3, 2, 3, 4, 4}, {2, 4, 5, 3, 1}, {6, 7, 1, 4, 5}, {5, 1, 1, 2, 4}});
deque<pair<int, int>> dq;
for (int i=0;i<heights.size();i++){
dq.push_back({i,heights[0].size()-1});
}
for (auto [u,v]: dq){
cout<<u<< " "<< v<<endl;
}
return 0;
}