C++ 关于从char数组转换为string-C++; #包括 #包括 #包括 使用名称空间std; int main(){ 字符串vet[10],辅助; std::向量名; 整数计数=0,总和=0; 而(count

C++ 关于从char数组转换为string-C++; #包括 #包括 #包括 使用名称空间std; int main(){ 字符串vet[10],辅助; std::向量名; 整数计数=0,总和=0; 而(count,c++,arrays,string,char,challenge-response,C++,Arrays,String,Char,Challenge Response,问题在于: #include <iostream> #include <string> #include <vector> using namespace std; int main () { string vet[10], aux; std::vector<char> name; int count=0, sum=0; while (count<10) { getline(cin, a

问题在于:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main () {
    string vet[10], aux;
    std::vector<char> name;
    int count=0, sum=0; 

    while (count<10) {
        getline(cin, aux);

        for (int i=0; i<aux.length(); i++)  name.push_back(aux[i]);

        for (int i=0; i<name.size(); i++) {
            name[i] = tolower(name[i]);                                 //para garantir que todos os caracteres estão em minúsculo  
            if (name[i] > 96 && name[i] < 123 && name[i] == 32)         //faixa de decimais das letras minúsculas e espaço em caso de nome composto
                sum += name[i];
        }

        char v[name.size()];                                            //vetor auxiliar criado para realizar a conversão de vetor de char para string

        for (int i=0; i<name.size(); i++)   {
            v[i] = name[i];
        }

        while (vet[sum%10] != "\0")                                     //para evitar colisão
            sum++;

        vet[sum%10] = (string) v;                                       //conversão para string e => K mod m = K % m em C++
        cout << vet[sum%10] << endl;
        count++;
        sum = 0;

        for (int i=0; i<name.size(); i++)   v[i] = '\0';

        name.clear();
    }

    cout << endl;

    for (int i=0; i<10; i++)    cout << vet[i] << endl;

    return 0;
}

正如指出的,这不是标准C++ +…/P> 无论如何,您可以通过以下方式进行修复:

char v[name.size()];
它或多或少与您的字符数组具有相同的效果,只是它不使用字符数组。

问题在于:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main () {
    string vet[10], aux;
    std::vector<char> name;
    int count=0, sum=0; 

    while (count<10) {
        getline(cin, aux);

        for (int i=0; i<aux.length(); i++)  name.push_back(aux[i]);

        for (int i=0; i<name.size(); i++) {
            name[i] = tolower(name[i]);                                 //para garantir que todos os caracteres estão em minúsculo  
            if (name[i] > 96 && name[i] < 123 && name[i] == 32)         //faixa de decimais das letras minúsculas e espaço em caso de nome composto
                sum += name[i];
        }

        char v[name.size()];                                            //vetor auxiliar criado para realizar a conversão de vetor de char para string

        for (int i=0; i<name.size(); i++)   {
            v[i] = name[i];
        }

        while (vet[sum%10] != "\0")                                     //para evitar colisão
            sum++;

        vet[sum%10] = (string) v;                                       //conversão para string e => K mod m = K % m em C++
        cout << vet[sum%10] << endl;
        count++;
        sum = 0;

        for (int i=0; i<name.size(); i++)   v[i] = '\0';

        name.clear();
    }

    cout << endl;

    for (int i=0; i<10; i++)    cout << vet[i] << endl;

    return 0;
}

正如指出的,这不是标准C++ +…/P> 无论如何,您可以通过以下方式进行修复:

char v[name.size()];

或多或少,它与您的字符数组具有相同的效果,只是它不使用字符数组。

听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看程序偏离预期的位置。这是一个非常重要的工具做任何编程。进一步阅读:仅供参考:
charv[name.size()]当数组大小不是常量表达式时,不是标准C++。IF语句在第19行中的条件永远不会是真的。intent@Bob__是的,它是| |。哈哈哈,这个想法是只接受小写字符和空格(ascii表上为32)。对于阅读本文的其他人:你们这些家伙完全不鼓励人们在未经通知的情况下提问。我认为我的帖子没有问题。听起来你们可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,你们可以一行一行地执行程序,看看它偏离了你们的预期。这是一个很好的例子n如果您要进行任何编程,请使用基本工具。进一步阅读:仅供参考:
charv[name.size()]当数组大小不是常量表达式时,不是标准C++。IF语句在第19行中的条件永远不会是真的。intent@Bob__是的,它是| |。哈哈哈,这个想法是只接受小写字符和空格(ascii表上为32)。对于阅读本文的其他人:你们这些家伙完全不鼓励人们在未经通知的情况下提问。我认为我的帖子没有问题。谢谢你的回答!请注意,
v
是完全多余的,你可以把它的每一句话都扔掉,只说
vet[sum%10]=name;
。我在直接使用vet[sum%10]时出现了这个错误=名称:
[Error]与“operator=”不匹配(操作数类型为“std::string{aka std::basic_string}”和“std::vector”)
我很确定你想使用
调整大小
而不是
保留
保留
只是分配内存。它根本不影响大小:@NathanOliver非常棒!谢谢你修改了类型!…eee打字错误。谢谢你的回答!注意
v
是完全多余的,你可以把每一个提到的内容都扔掉当直接使用vet[sum%10]=name:
[error]与“operator=”不匹配(操作数类型为“std::string{aka std::basic_string}”和“std::vector”)时,我会遇到这个错误
我很确定您想使用
调整大小
而不是
保留
保留
只分配内存。它根本不影响大小:@NathanOliver非常棒!感谢您修复类型!…eee打字错误。