连接字符串的意外问题

连接字符串的意外问题

本文介绍了连接字符串的意外问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用+连接字符串,但是发生了一些奇怪的事情.这是我为某班级项目准备的成绩"班级:

I'm trying to concatenate strings using +, but there is some weird stuff going on. Here is my "Grade" class I have for a class project:

#pragma once
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Grade {
    private:
        string className;
        string student;
        string letter;

    public:
        Grade(string c, string s, string l) : className(c), student(s), letter(l) {}

        string getLetterGrade() const { return letter; }
        string getClassName() const { return className; }
        string getStudent() const { return student; }
        void setLetterGrade(string l) { letter = l; return;}
        void setClassName(string c) { className = c; return;}
        void setStudnet(string s) { student = s; return;}

        string toString() const { string output = "hello"+student; return output; }
};

很明显,toString()方法当前不是我想要的.如果按上述方式运行toString(),则会得到"hello529173860",这与预期的一样.但是,如果我将行更改为:

Obviously, the toString() method isn't currently what I want it to be.If I run the toString() as above, I get "hello529173860" out, as expected. However, if I change the line to:

string toString() const { string output = student+"hello"; return output; }

则输出为"hello3860".这不仅是将hello字符串放在最前面,而且还可以在此过程中替换学生字符串中的字符……以某种方式?

then the output is "hello3860". This isn't just putting the hello string on the front, but its replacing characters from the student string in the process... somehow?

此外,如果我尝试使用:

Furthermore, if I try to use:

string toString() const { string output = "hello"+" world"; return output; }

我得到一个错误:

Grade.h: In member function ‘std::string Grade::toString() const’:
Grade.h:29:53: error: invalid operands of types ‘const char [6]’ and ‘const char [7]’ to binary ‘operator+’
   string toString() const { string output = "hello"+" world"; return output; }
                                                     ^

我真的不知所措...特别是因为我在程序的前面完成了字符串连接而没有任何问题.我想要的是输出类似的内容:

I'm really at a loss for what it going on here... especially since I have done string concatenation earlier in the program without issue.What I would like is to output something like:

学生+ [一些空白] +字母+ [一些空白] + className"

"student+[some white space]+letter+[some white space]+className"

推荐答案

可以将 std :: string 添加到任何内容(另一个 std :: string ,带引号的字符串文字 char ),并提供直观的结果, 但是 ,如果您尝试将双引号的字符串文字添加到另一个字符串文字或 char ,那么它将无法运行":

A std::string can be added to anything (another std::string, double-quoted string literal, char) and provide intuitive results, but if you try to add a double-quoted string literal to another string literal or a char then it won't "work":

  • 添加到 char 或其他整数值的字符串文字将经过标准转换为 const char * ,然后添加到指针的任何数字都将移动沿字符数的字面量:如果偏移量不在字符串字面量之内,则如果取消引用(使用)结果指针,则会得到未定义的行为,

  • a string literal added to a char or other integral value will undergo Standard Conversion to a const char*, then any number added to the pointer will move along the literal that number of characters: if the offset isn't inside the string literal then you get undefined behaviour if you dereference (use) the resultant pointer,

即使衰减到两个指针后,也无法添加两个字符串文字,因此您会遇到编译时错误.

two string literals just can't be added, even after decaying to two pointers, so you'll get a compile-time error.

有时候,您可能希望显式构造一个 std :: string ,以便与其他值的连接可以按您的意愿工作: my_string = std :: string("hello")+ my_const_char_ptr +'\ n'.

Sometimes you will want to explicitly construct a std::string so that concatenation with other values works as you'd like: e.g. my_string = std::string("hello ") + my_const_char_ptr + '\n'.

示例:

std::string s = "Hello";

s + " World"; // ok
"Hello " + s; // ok
"Hello " + "World"; // NOT ok - two string literals
s += " World"; // ok
s += " Goodbye " + "World"; // NOT ok - "+" evaluated before "+="
s += std::string(" Goodbye ") + "World"; // OK - right-hand-side no longer two literals
                                         // BUT may be slower than two "s +="

这篇关于连接字符串的意外问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 13:20