What is the use i mean why do we use STL???if we have all the algorithms …why do we use of STL and plus how many are there??? is there any website on which i can find out so i can use STL
Algorithm Standard Template Library
Hi @akshat42bajpai
STL is there just to make some things simpler… different stl functions need different header files…
Suppose u have an array and u want to sort it, then using stl u can perform that operation in just one line… u can google can find more about different stl functions available…
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
This is a leetcode question vaibhav …i haven’t studied OOPS or vector yet can you please tell me how should i do this using array
This is my code for the above question can you please help me
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> v;
for(int i=0;i<nums.size()-1;i++){
for(int j=i+1;j<nums.size();j++){
if(nums[i] + nums[j] == target){
v.push_back(i);
v.push_back(j);
break;
}
}
}
return v;
}
this is a simple brute force way…
vaibhav one more question is that i have still not covered a lot of DSA…i wanted to know that whenever i open leetcode or gfg to solve questions…the code editor already has syntax written as class public void { and then we have to write the code in those curly braces… i wanted to know that i am still on bitmasking…but still till now i don’t write code with this syntax???/ In which upcoming particular topic i will be covering this stuff?
and the code you send me how do i take the user input if the size is undefined??
hi @akshat42bajpai
so basically what happens on leetcode and geeksforgeeks is that u are just provided with the function part… u don’t have to write the code from scratch…
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
vector<int> v;
for(int i=0;i<n;i++){
int no;
cin>>no;
v.push_back(no);
}
}
okay but what about that text of class public void…in which topic i will be covering that?? and if i want to earse that and write the code in my way can i do that??
hi @akshat42bajpai
actually what u are taking about is not taught anywhere… as i said its just function, which u have to complete… rest i dint think u can erase there and write code from scratch… if u want to write code from scratch then go for sites like codechef, codeforces…
#include<bits/stdc++.h>
using namespace std;
vector TwoSum(vector&nums, int target){
vector v;
for(int i=0;i<nums.size();i++){
for(int j=i+1;j<nums.size();j++){
if(nums[i] + nums[j]==target){
v.push_back(i);
v.push_back(j);
break;
}
}
}
return v;
}
int main(){
int n;
cin>>n;
vector nums;
for(int i=0;i<n;i++){
int no;
cin>>no;
nums.push_back(no);
}
int target;
cin>>target;
result = TwoSum(nums, target);
cout<<result;
}
it’s still not working vaibhav can you please help??
this is the code if you can help it’s the same leetcode question
hi @akshat42bajpai
U are returning vector from function… so in main write as
vector<int> result
and for printing and traverse over result vector…
I did it what you said and in coding blocks IDE the output is coming as well but in leetcode IDE it is showing this error
Line 51: Char 25: error: use of undeclared identifier ‘Solution’
vector ret = Solution().twoSum(param_1, param_2); return ret;
^
hi @akshat42bajpai
its working fine…
#include<bits/stdc++.h>
using namespace std;
vector<int> TwoSum(vector<int>&nums, int target){
vector<int> v;
for(int i=0;i<nums.size();i++){
for(int j=i+1;j<nums.size();j++){
if(nums[i] + nums[j]==target){
v.push_back(i);
v.push_back(j);
break;
}
}
}
return v;
}
int main(){
int n;
cin>>n;
vector<int> nums;
for(int i=0;i<n;i++){
int no;
cin>>no;
nums.push_back(no);
}
int target;
cin>>target;
vector<int> result = TwoSum(nums, target);
for(int x:result){
cout<<x<<" ";
}
}