简体   繁体   English

为什么 String 返回垃圾值

[英]Why String is returning Junk values

Here I am using Data Structure Queues to change cards places using strings.在这里,我使用数据结构队列来使用字符串更改卡片位置。 User picks the top card than put it at last of the deck.用户选择最上面的牌而不是将其放在牌组的最后。 The problem is that I need to return updated string to main.问题是我需要将更新后的字符串返回给 main。 What it is returning is junk Values.它返回的是垃圾值。 I also tried to copy the string array and than returning that arraybut didn't work.我还尝试复制字符串数组而不是返回该数组但没有用。 Can anyone help?谁能帮忙?

#include<iostream>
#include<string>
using namespace std;
string CommunityChest(string Cards[]){
    struct Que{
     string cards[6];
     int front =0;
     int back=-1;
    }ComQ;
    string temp;

    for(int i=0; i<5;i++)
    {//Enqueue(Cards)
        ComQ.back++;
        if(ComQ.back<=5)
        {
            ComQ.cards[ComQ.back]=Cards[i];
        }
        else{
            cout<<"Overflow";
        }
    }
    //Display
    for(int i=ComQ.front; i<=ComQ.back; i++)
    {
        cout<<ComQ.cards[i]<<endl;
    }
    //Del()
    if(ComQ.front>=0)
    {
        temp=ComQ.cards[ComQ.front];
        ComQ.front++;
    }
    cout<<endl<<"Pick the top Card"<<endl;
    cout<<temp<<endl;
    //EnQ the picked card
    ComQ.back++;
    if(ComQ.back<=5)
        {
            ComQ.cards[ComQ.back]=temp;
        }
        else{
            cout<<"Overflow";
        }
        cout<<endl<<"After Inserting top card:"<<endl;
        //Display
        string newQ1[5];
    for (int i=ComQ.front; i<=ComQ.back; i++)    //Making an alternate array to copy Comq.cards array data
    {
        newQ1[i]=ComQ.cards[i];            
    }
    for(int i=0; i<5; i++)
    {
        return newQ1[i];          //trying to return string array
        //cout<< newQ1<<endl;
    }

}
int main(){

string cards[5]{ "1 Advance ........",
                 "2. It is your ...........",
                 "3. You have won .............",//Cards as strings
                 "4. From sale of..............",
                 "5. Pay Hospital............"};
       string newQ1=CommunityChest(cards);
       for(int i=0; i<5; i++)
       cout << newQ1[i] << endl;
return 0;
}


There are some issues with this code as mentioned in the comments, you are trying to do a return in a loop, this may look okay but it's wrong since a return can get you out of the function. But it seems like you want to return an array of strings.如评论中所述,此代码存在一些问题,您正在尝试在循环中返回,这看起来不错,但这是错误的,因为返回可以使您脱离 function。但您似乎想返回字符串数组。

A fix for this function would be like this:这个 function 的修复是这样的:

#include <iostream>

std::string* CommunityChest(std::string Cards[]) {
    struct Que {
        std::string cards[6];
        int front = 0;
        int back = -1;
    } ComQ;
    std::string temp;

    for (int i = 0; i < 5; i++) { // Enqueue(Cards)
        ComQ.back++;
        if (ComQ.back <= 5) {
            ComQ.cards[ComQ.back] = Cards[i];
        }
        else {
            std::cout << "Overflow";
        }
    }
    // Display
    for (int i = ComQ.front; i <= ComQ.back; i++) {
        std::cout << ComQ.cards[i] << std::endl;
    }
    // Del()
    if (ComQ.front >= 0) {
        temp = ComQ.cards[ComQ.front];
        ComQ.front++;
    }
    std::cout << std::endl
         << "Pick the top Card" << std::endl;
    std::cout << temp << std::endl;
    // EnQ the picked card
    ComQ.back++;
    if (ComQ.back <= 5) {
        ComQ.cards[ComQ.back] = temp;
    }
    else {
        std::cout << "Overflow";
    }
    std::cout << std::endl
         << "After Inserting top card:" << std::endl;
    // Display
    // Creating a dynamic array to store the values
    std::string* newQ1 = new std::string[5];
    for (int i = ComQ.front, j = 0; i <= ComQ.back && j < 5; i++, j++) {// Making an alternate array to copy Comq.cards array data
        newQ1[j] = ComQ.cards[i];
    }
    return newQ1; // Returning the dynamic array
}
int main()
{
    std::string cards[5]{
        "1 Advance ........",
        "2. It is your ...........",
        "3. You have won .............", // Cards as strings
        "4. From sale of..............",
        "5. Pay Hospital............"
    };
    std::string* newQ1 = CommunityChest(cards);
    for(int i = 0; i < 5; i++) {
        std::cout << newQ1[i] << std::endl;
    }
    delete[] newQ1; // Deleting the array
    return 0;
}

The output will be: output 将是:

1 Advance ........
2. It is your ...........
3. You have won .............
4. From sale of..............
5. Pay Hospital............

Pick the top Card
1 Advance ........

After Inserting top card:
2. It is your ...........
3. You have won .............
4. From sale of..............
5. Pay Hospital............
1 Advance ........

In this fix, I'm returning a dynamically allocated array of strings because a static array will be destroyed once the scope ends, it would be better sometimes to use other ways to allocate memory such as std::unique_ptr<> or std::shared_ptr<> , but I'd suggest you learn how to do it yourself first then use those when needed.在此修复中,我返回一个动态分配的字符串数组,因为一旦 scope 结束,static 数组将被销毁,有时使用其他方式分配 memory 会更好,例如std::unique_ptr<>std::shared_ptr<> ,但我建议您先自己学习如何做,然后在需要时使用它们。

EDIT: You can also return an std::array<> , I suggest you to read about it as C-Style arrays cannot be returned in you way that you tried and can't be returned without using dynamic allocation, so an std::array<> can be a good replacement over std::string* in this case编辑:你也可以返回一个std::array<> ,我建议你阅读它作为 C-Style arrays cannot be returned in your way that you tried and cannot be returned without using dynamic allocation, so std::array<>在这种情况下可以很好地替代 std::string*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM