0
#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?

Najat
  • 167
  • 14

1 Answers1

0

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.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
lxyscls
  • 297
  • 1
  • 3
  • 17