排序功能不起作用c ++

时间:2017-02-24 10:20:16

标签: c++ arrays

所以我是c ++的新手,我想知道如何使用sort函数。 这是我的代码,并且由于以下原因而无法正常工作:

#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    ifstream in("date.in");
    ofstream out("date.out");
    int v[5]= {2, 3 ,1, 0, 5};
    sort(begin(v), end(v));
    for(int j=0; j<5; j++){
        out<<v[j]<<" ";
    }
    return 0;
}

我得到的错误代码是:

  

错误:&#39;开始&#39;未在此范围内声明

== EDIT ==

问题在于我没有使用c ++ 14。

4 个答案:

答案 0 :(得分:3)

您需要加入@Component({ selector : 'maps-test', template : '<div #map></div>' }) export class MapsTestComponent { @ViewChild('map') mapElement: ElementRef; constructor(private ngZone: NgZone){} ngAfterViewInit() { loadMap(); } loadMap(){ Geolocation.getCurrentPosition().then((position) => { let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); let mapOptions = { center: latLng, zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP } this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions); let service = new google.maps.places.PlacesService(this.map); service.nearbySearch({ location: latLng, radius: 500, type: ['pub'] }, (results, status) => { this.callback(results, status) }); }, (err) => { console.log(err); }); } callback(results, status) { if (status === google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i < results.length; i++) { this.createMarker(results[i]); } } } createMarker(place){ var placeLoc = place.geometry.location; var marker = new google.maps.Marker({ map: this.map, position: place.geometry.location }); let infowindow = new google.maps.InfoWindow(); google.maps.event.addListener(marker, 'click', () => { this.ngZone.run(() => { infowindow.setContent(place.name); infowindow.open(this.map,this); }); }); } addMarker(){ let marker = new google.maps.Marker({ map: this.map, animation: google.maps.Animation.DROP, position: this.map.getCenter() }); let content = "<h4>You are here!</h4>"; this.addInfoWindow(marker, content); } addInfoWindow(marker, content){ let infoWindow = new google.maps.InfoWindow({ content: content }); google.maps.event.addListener(marker, 'click', () => { this.ngZone.run(() => { infoWindow.open(this.map, marker); }); }); } } 。因为#include <iterator>定义了std::begin

或者将类型从array更改为vector,您的代码就可以使用。

答案 1 :(得分:2)

有两种解决方案。

std :: sort将迭代器作为序列的开头,并在序列结束后一对一。这是stl算法的惯例,它适用于集合。

所以

 std::vector v = {2, 3, 0, 1, 5};
 sort(v.begin(), v.end());

 int v[5] = {2, 3, 0, 1, 5};
 sort(v, v + 5);

普通指针是迭代器以及在stl头文件中声明的stl :: iterator类型。

答案 2 :(得分:0)

sort(begin(v), end(v));替换为sort(v, v + 5)); v数组,不是向量。对于向量,请使用sort(v.begin(), v.end());

答案 3 :(得分:-1)

在代码中添加以下行:

#include<algorithm>