对浮点指针数组进行排序

时间:2016-12-29 10:59:50

标签: c++ arrays sorting pointers std

我们有:

 private void function(ptr* data, const int size_of_data){
       std::sort(std::begin(data), std::end(data), floatcomparer);

这实际上可行吗?还有另外一种方法吗?因为我到这里error: no instance of overloaded function ""std::begin" matches the argument types are: (float *). 我知道我们需要一个快速排序算法的大小,如std用途和ptr *不提供包含的大小。但是不应该有一种方法因为我知道我指向的数组有多大?

2 个答案:

答案 0 :(得分:1)

    class SentimentAnalyzer {

        public TweetWithSentiment findSentiment(String line) {

        if(line == null || line.isEmpty()) {
          throw new IllegalArgumentException("The line must not be null or empty.");
        }

        Annotation annotation = processLine(line);

        int mainSentiment = findMainSentiment(annotation);

        if(mainSentiment < 0 || mainSentiment > 4) { //You should avoid magic numbers like 2 or 4 try to create a constant that will provide a description why 2
           return null; //You should avoid null returns 
        }

        TweetWithSentiment tweetWithSentiment = new TweetWithSentiment(line, toCss(mainSentiment));
        return tweetWithSentiment;

    }

    private String toCss(int sentiment) {
        switch (sentiment) {
        case 0:
            return "very negative";
        case 1:
            return "negative";
        case 2:
            return "neutral";
        case 3:
            return "positive";
        case 4:
            return "very positive";
        default:
            return "default";
        }

     }


     private int findMainSentiment(Annotation annotation) {

        int mainSentiment = Integer.MIN_VALUE;
        int longest = Integer.MIN_VALUE;


        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {

            for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {

                String word = token.get(CoreAnnotations.TextAnnotation.class);
                String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
                String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
                String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);

                System.out.println("word: " + word);
                System.out.println("pos: " + pos);
                System.out.println("ne: " + ne);
                System.out.println("Lemmas: " + lemma);

            }      

           int sentenceLength = String.valueOf(sentence).length();

           if(sentenceLength > longest) {

             Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);

             mainSentiment = RNNCoreAnnotations.getPredictedClass(tree);

             longest = sentenceLength ;

            }
        }

        return mainSentiment;

     }


     private Annotation processLine(String line) {

        StanfordCoreNLP pipeline = createPieline();

        return pipeline.process(line);

     }

     private StanfordCoreNLP createPieline() {

        Properties props = createPipelineProperties();

        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        return pipeline;

     }

     private Properties createPipelineProperties() {

        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, sentiment");

        return props;

     }


 }

您可以在此表单中使用传递std::sort(arr, arr+ size, std::greater<float>());

float arr

使用此比较器,您可以对其进行排序。

答案 1 :(得分:1)

您的功能签名错误,并且您错误地调用了std::sort,您想要的更像是:

void function(float* data, std::size_t size_of_data) {
    std::sort(data, data + size_of_size, std::less<float>());
}