C++ Notes For STL
C++ Notes For STL
-------------------------------------------------
=>
stirng temp;
getline(cin,temp);
https://www.geeksforgeeks.org/getline-string-c/
Now there is one problem the getline consumes the \n or ENTER key also
so if the sequence is
int n;
cin>>n;
getline(cin,temp);
then the ENTER pressed after cin>>n will be consuemd by the getline
so remember to flush the \n or ENTER .
i.e
*****
int test;
cin>>test;
string test,flush;
getline(cin,flush);
getline(cin,test);
----------------------------------------------------------------------------
=>
QUESTION = > https://leetcode.com/problems/largest-number/
METHOD 1:
class Solution {
public:
static bool comp(int n1,int n2){
string s1 = to_string(n1);
string s2 = to_string(n2);
return (s1+s2> s2+s1);
}
string largestNumber(vector<int>& nums) {
int n =nums.size();
sort(nums.begin(),nums.end(),comp);
string result="";
for(int i=0;i<n;i++){
result+=to_string(nums[i]);
}
if(result[0]=='0'){
return "0";
}
return result;
}
};
METHOD 2:
class Solution {
struct comp {
bool operator() (int a, int b) {
string comb1 = to_string(a) + to_string(b);
string comb2 = to_string(b) + to_string(a);
return comb1 > comb2;
}
} mycomp;
public:
string largestNumber(vector<int>& nums) {
sort(nums.begin(), nums.end(), mycomp);
if (nums[0] == 0) return "0";
string res = "";
for (auto num : nums) {
res = res + to_string(num);
}
return res;
}
};
----------------------------------------------------------------------------
int m = grid.size();
int n = grid[0].size();
vector<vector<bool>>visited(m, vector<bool>(n, false));
* int V = graph.size();
vector<int>res;
vector<int>dp(V, 0);
----------------------------------------------------------------------------
https://www.geeksforgeeks.org/deque-cpp-stl/
----------------------------------------------------------------------------
Overview:->
----------------------------------------------------------------------------
6> unordered_set
=>
https://www.geeksforgeeks.org/unordered_set-in-cpp-stl/
----------------------------------------------------------------------------
7> set
=>
https://www.geeksforgeeks.org/insertion-deletion-stl-set-c/
----------------------------------------------------------------------------
8> unordered_map
=>
https://www.geeksforgeeks.org/unordered_map-in-cpp-stl/
----------------------------------------------------------------------------
----------------------------------------------------------------------------
One more problem here is on compairing 9.58<9.58 was giving true i think this is
just float point comparision problem, i kept it integer and compared it with 958
float k1,k2,k3,v,t;
cin>>k1>>k2>>k3>>v;
v = v*k1*k2*k3;
t = 100/v;
t = round(t*100);
t = t/100;
t = t*100;
cout<<t<<endl;
if(t < 958){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
----------------------------------------------------------------------------