Magician_liu 2023-05-16 21:32 采纳率: 57.1%
浏览 38
已结题

请问是什么原因造成打印C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC

打分案例如下:


#include<iostream>
using namespace std;
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
class student
{
public:
    string name;
    int mscore;

};
vector<student> s;
deque<int> a;
void createstudent(vector<student>&s)
{
    
    string a = "abcde";
    for (int i = 0; i < 5; i++)
    {
        student m;
        m.name = "学生" + a[i];
        m.mscore = 0;
        s.push_back(m);
    }
    /*for (int i = 0; i < 5; i++)
    {
        student stu;
        stu.name = "学生";
        stu.name += a[i];
        stu.mscore = 0;
        s.push_back(stu);
    }*/
}
void teacherset(deque<int>&a)
{
    srand(time(0));
    for (vector<student>::iterator it = s.begin(); it != s.end(); it++)
    {
        
        for (int i = 0; i < 10; i++)
        {    
            int m = rand() % 100;
                
            a.push_back(m);
        }
        sort(a.begin(), a.end());
        
        a.pop_back();
        a.pop_front();
        int score = 0;
        for (deque<int>::iterator it = a.begin(); it != a.end(); it++)
        {
            score += *it;
        }
        int meanscore = score / a.size();
        it->mscore = meanscore;
    }
}
bool compare(student& a, student& b)
{
    return(a.mscore > b.mscore);
}
void printprize(vector<student>& a)
{
    sort(a.begin(), a.end(), compare);
    for (vector<student>::iterator it = a.begin(); it != a.end(); it++)
    {
        cout << "姓名:" << it->name << "成绩:" << it->mscore << endl;
    }

}


void test() {
    //创建学生
    createstudent(s);
    //老师打分
    teacherset(a);
    //打印排名
    printprize(s);
}

int main()
{
    test();
}

代码中:

for (int i = 0; i < 5; i++)
    {
        student m;
        m.name = "学生" + a[i];
        m.mscore = 0;
        s.push_back(m);
    }

产生的结果是
姓名:成绩:47
姓名:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\vector成绩:45
姓名:成绩:43
姓名:成绩:43
姓名::\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\vector成绩:42

而将这段代码改成:

for (int i = 0; i < 5; i++)
    {
        student stu;
        stu.name = "学生";
        stu.name += a[i];
        stu.mscore = 0;
        s.push_back(stu);
    }

程序才正常打印如下:
姓名:学生b成绩:55
姓名:学生d成绩:55
姓名:学生a成绩:54
姓名:学生e成绩:54
姓名:学生c成绩:53
问题:请问是什么原因造成打印C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\vecto成这样,即姓名无法成功打印。直接(stu.name="学生"+a[i];)与(stu.name="学生";stu.name+=a[i];)有什么区别吗?会造成如此错误?

  • 写回答

2条回答 默认 最新

  • 小果沐雨 2023-05-17 09:23
    关注

    string是类,其中包含重载运算符 +=,=等函数,赋值时候属于调用的函数。 stu.name = "学生"; stu.name += a[i];是字符串赋值,拼接。
    "学生" 是 const char[5],a[i]是char,两个类型拼接会产什么结果?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月25日
  • 已采纳回答 5月17日
  • 创建了问题 5月16日

悬赏问题

  • ¥15 AnolisOs7.9如何安装 Qt_5.14.2的运行库
  • ¥20 求:怎么实现qt与pcie通信
  • ¥50 前后端数据顺序不一致问题,如何解决?(相关搜索:数据结构)
  • ¥15 基于蒙特卡罗法的中介效应点估计代码
  • ¥15 罗技G293和UE5.3
  • ¥20 Tesla 特斯拉K80显卡 如果需要使用该设备,你需要禁用系统上的另一个设备。
  • ¥30 QT调用百度智能云千帆模型无法取得返回文本
  • ¥50 CCD工业视觉相机检测出现光边
  • ¥60 二次元手游日常任务自动化代肝(相关搜索:自动化)
  • ¥15 mysql将查询的结果作为动态列名怎么实现