#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, j, t;
vector <int> v;
scanf("%d", &t);
while(t--) {
scanf("%d", &j);
v.push_back(j);
}
if(is_sorted(v.begin(), v.end()))
printf("Sorted\n");
else
printf("Unsorted\n");
return 0;
}
Here's my C++ code to check whether a vector is sorted or not. But my IDE (Code Blocks) doesn't compile it and gives the message "is_sorted was not declared in this scope". What's wrong with this code?
You need to add the compile flag.
[root#router ~]# cat t.cpp
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, j, t;
vector <int> v;
scanf("%d", &t);
while(t--) {
scanf("%d", &j);
v.push_back(j);
}
if(is_sorted(v.begin(), v.end()))
printf("Sorted\n");
else
printf("Unsorted\n");
return 0;
}
[root#router ~]# g++ -o t t.cpp -std=c++11
Here is the std::is_sorted reference.
Related
this is the program
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main() {
clrscr();
cout<<"Hello, World!";
getch();
}
fatal error: conio.h: No such file or directory #include <conio.h>sdfs
getch return a value
as follows
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main() {
clrscr();
cout<<"Hello, World!";
char a = getch();
}
why am i geting runtime error in one
thats my question i am writing this simply because it is showing to lksdjflksjflkjsdflk
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long int L;
long int l;
cin>>L;
cin>>l;
long int ans=L%l;
if(L>=l)
{
cout<<ans;
}
else
{
cout<<L;
}
return 0;
}
Unless you provide the inputs for L,l and runtime error description... It's ambiguous and we have to guess.. hope you are not passing 0 to 'l'..
I am creating a small C++ program for homework. I im trying to populate a 2D vector but when I write matriz[iA][iB]=iNum; it gives me the error "no match for 'operator='"
#include <iostream>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <iomanip>
#include <set>
#include <vector>
#include <map>
using namespace std;
void popularMatriz(int iTamano, vector<vector<int>> *matriz){
for(int iA=0; iA<iTamano; iA++){
for(int iB=0; iB>iTamano; iB++){
int iNum;
scanf("%d", &iNum );
matriz[iA][iB]=iNum;
}
}
}
int main(){
int iTamano;
scanf("%d", &iTamano);
vector<vector<int>> matriz(iTamano, vector<int>(iTamano));
matriz[2][2]=5;
popularMatriz(iTamano, &matriz);
return 0;
}
You're passing a pointer to matriz; so is wrong use it as
matriz[iA][iB]=iNum;
I suggest you to pass it as reference; I mean, define popularMatriz() as
void popularMatriz(int iTamano, vector<vector<int>> & matriz)
and call it without &
popularMatriz(iTamano, matriz);
You're taking a pointer to matriz. Do this instead:
(*matriz)[iA][iB]=iNum;
This question already has an answer here:
invalid operator < while sorting std::list
(1 answer)
Closed 7 years ago.
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
bool fun(int i, int j){
return abs(i - j) != -1;
}
int main(){
vector <int> v = { 1, 2, 3, 4, 5 };
sort(v.begin(), v.end(), fun);
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
return 0;
}
When I use the comparator 'fun' the programs throws the exception 'invalid operator <'. How can I modify this function to make the program run normally?
Your fun function does not provide strict weak ordering. If i and j are equal, it will return true. So you are not following the rules. Your implementation of the standard library responds by throwing an exception.
How can I modify this function to make the program run normally?
Assuming you want to sort in ascending order. Just use operator <.
bool fun(int i, int j)
{
return i < j;
}
Alternatively you could just use the comparator provided by the standard.
sort(v.begin(), v.end(), std::less<int>());
See the link in the comments or the answer provided by #DanielDaranas to understand why your original function doesn't work.
I got an error of C:\temp\hashTableProject\main.cpp|14|undefined reference to hash::Hash(std::string)
Anyone know how to solve this problem?
hash.h
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef HASH_H
#define HASH_H
class hash{
public:
int Hash(string key);
};
#endif // HASH_H
hash.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int hash::Hash(string key){
//int hash = 0;
int index;
index = key.length();
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int main()
{
int index;
hash hashOb;
string traget = "Testing";
index = hashOb.Hash(traget);
cout << index << endl;
return 0;
}
Im using CodeBlock 13.12
There is only main.o file in obj folder. I dont know why the hash.o isn't there.
hash is an inbuilt template in "std" namespace.
Try removing using namespace std; lien and use namespace name as and when required.