小编use*_*625的帖子

for循环c ++'toupper'实现

有人可以解释为什么C++中的这个短代码不会产生预期的输出.代码应该用大写字母打印字符串.

#include <iostream>
#include <string>
using namespace std;

int main(){
    string sample("hi, i like cats and dogs.");
    cout << "small: " << sample << endl << "BIG  : ";

    for(char c: sample)
        cout << toupper(c);
    cout<<endl;

return 0;
}
Run Code Online (Sandbox Code Playgroud)

上述程序的输出是:

small: hi, i like cats and dogs.
BIG  : 72734432733276737569326765848332657868326879718346
Run Code Online (Sandbox Code Playgroud)

但我期待:

small: hi, i like cats and dogs.
BIG  : HI, I LIKE CATS AND DOGS.
Run Code Online (Sandbox Code Playgroud)

我只用python编程.

c++ toupper

1
推荐指数
1
解决办法
751
查看次数

使用Collections.sort()时出错

我想知道为什么我在尝试对List进行排序后出现错误.当我尝试对包含"Student"对象的列表进行排序时,会发生错误.

import java.lang.reflect.Array;
import java.util.*;
import java.util.ArrayList;

public class mainProgram {
    public static void main(String[] args) {
        List<Student> classStudents = new ArrayList<Student>();

    //Add 4 students to List
        classStudents.add(new Student("Charles", 22));
        classStudents.add(new Student("Chris", 25));
        classStudents.add(new Student("Robert", 23));
        classStudents.add(new Student("Adam", 21));

    //sort and print
        Collections.sort(classStudents);    //Why does this not work?
        System.out.println("classStudent(Sorted) ---> "
        + Arrays.toString(classStudents.toArray()));
    }
}
Run Code Online (Sandbox Code Playgroud)
class Student implements Comparable<Student>{
    // fields
    private String name;
    private int age;

    //Constructor
    public Student(String name, int age){ 
        name = name;
        age = age;
        } …
Run Code Online (Sandbox Code Playgroud)

java sorting comparable comparator

1
推荐指数
1
解决办法
4268
查看次数

标签 统计

c++ ×1

comparable ×1

comparator ×1

java ×1

sorting ×1

toupper ×1