C++ 我该怎么说呢;return";函数之间的字符串数组?

C++ 我该怎么说呢;return";函数之间的字符串数组?,c++,C++,嘿,当我尝试用return传递一个字符串值时,我正在试验我所知道和意识到的东西,这是不受支持的,知道吗?很抱歉,如果我的代码是noob风格的(我只有2个月的经验),我计划在函数之间拆分代码,但我似乎无法这样做,因为返回字符串数组不能用return完成:(代码如下: #include <iostream> #include <math.h> #include <sstream> using namespace std; int itemlist=0;

嘿,当我尝试用return传递一个字符串值时,我正在试验我所知道和意识到的东西,这是不受支持的,知道吗?很抱歉,如果我的代码是noob风格的(我只有2个月的经验),我计划在函数之间拆分代码,但我似乎无法这样做,因为返回字符串数组不能用return完成:(代码如下:

    #include <iostream>
#include <math.h>
#include <sstream>
using namespace std;
int itemlist=0;


int c=0;
int r = 0;



int itemlistdec()
{
cout<<"How many items would you like to input?";
cin>>itemlist;  
return itemlist;
}


int main() {


itemlistdec();
string item[4][itemlist];//declares item and that columns equal to itemlist whose content is declared right above

    for (int c=0;c<itemlist;c++)
    {

        for (int r=0;r<3;r++) //DETERMINES WHERE EACH RECORD GOES
        {
        if (r==0)
        {

        cout<<"Please enter the name of the item ";
        }

        if (r==1)
        {
            cout<<"Please input the buying price\n";
        }

        if (r==2)
        {
            cout<<"Please input the selling price\n";
        }

            cin>>item[r][c];

        }
    }

    int calc[3][itemlist];//declaring calc and itemlist

    for (int r = 0;r<itemlist;r++)
    {
        istringstream(item[1][r])>>calc[0][r]; //TAKES BUYING PRICE INTO INT ARRAY FOR CALCULATION

    }

    for (int r = 0;r<itemlist;r++)
    {
        istringstream(item[2][r])>>calc[1][r]; //TAKES SELLING PRICE INTO INT ARRAY FOR CALCULATION

    }

    for (int fart = 0;fart<itemlist;fart++)
    {
        calc[2][fart] = calc[1][fart] - calc[0][fart]; //REPEATS CALCULATION FOR PROFIT UNTIL ITEMLIST IS REACHED
    }


    for (int r = 0;r<itemlist;r++)
    {

    stringstream ss;
    ss<<calc[2][r]; //CONVERTS BOTH PROFIT VALUES INTO STRINGS
    item[3][r] = ss.str();

    }


    cout<<"______________________________________________\n"; //DISPLAYS OUTPUT IN TABLE FORM
    cout<<"Item\t\tBuying Price\t\tSelling Price\t\tProfit\n";

        for (int c=0;c<itemlist;c++)
    {


        for (int r=0;r<4;r++)
        { 
            cout<<item[r][c]<<"\t\t";
            if (r==1)
            {
                cout<<"\t";
            }
            if (r==2)
            {
                cout<<"\t";
            }
            if (r==3)
            {
                cout<<"\n";
            }
        }
    }




    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int itemlist=0;
int c=0;
int r=0;
int itemlistdec()
{
库特姆利斯特;
返回项目列表;
}
int main(){
itemlistdec();
string item[4][itemlist];//声明item以及与itemlist相等的列,其内容声明在正上方

对于(int c=0;c我认为你可以使用向量,它非常强大。比如:

std::vector<std::vector<std::string> > mySecondVector;
std::vector<std::string> myFirstVector;
myFirstVector.push_back("MyString");
mySecondVector.push_back(myFirstVector);
mySecondVector[i][j]; // to access
std::vector mySecondVector;
std::vector myFirstVector;
myFirstVector.push_back(“MyString”);
mySecondVector.push_back(myFirstVector);
mySecondVector[i][j];//访问

对于add,访问返回数组的

上的元素监视有点愚蠢。必须将其作为指针返回,一旦返回,就会丢失所有大小信息。itemlist将其作为全局变量处理,但全局变量会施加其自身的一组限制。首先,您不能有超过这些数组中的一个(除非它们都是相同大小的),因为多个数组将在同一位置存储它们的长度

首先,您必须翻转数组的方向,以获得最右边索引上的恒定大小。坦率地说,由于位置的原因(关于一个项的所有数据都在同一个内存区域中,因此,如果您将一个项的一部分加载到缓存中,您可能会加载所有项。这几乎总是会导致更快的程序)无论如何,这样可能更好。然后您可能已经注意到,如果编译器不向您咆哮,您不能只返回
string[][]
或任何类似的内容,因此您必须玩定义可以返回的自定义数据类型的游戏

typedef string (*arrayPtr)[4];    
现在你可以试试了

arrayPtr itemlistdec()
{
    cout << "How many items would you like to input?";
    cin >> itemlist;

    string item[itemlist][4]; 
    //load the array

    return item;
}
现在我们有了一些有效的方法。我们可以通过以下方法使它更具可读性(同时也会减少可读性,因为读者必须跟踪
数组是什么,才能知道如何使用它)

请注意,我正在返回局部向量,并依靠移动语义在返回时移动向量,而不是复制它。还请注意,您不再需要全局项列表,因为向量知道它有多大。如果您愿意,您甚至可以稍后向它添加内容。还请注意,我在输入上放弃了内部for循环。这是不需要的

这比数组选项更有效,但我仍然不喜欢它。Item正在调用它自己的类,以便我们可以利用面向对象编程带来的好处。我们还可以为所有成员变量指定正确的数据类型。这样,我们可以在用户输入时捕获错误输入,并消除额外的整数arr计算所使用的ay

class Item
{
public:
    void read(istream & in)
    {
        // ignoring possibility of total failure of cin here.
        cout<<"Please enter the name of the item ";
        in >> name;
        cout<<"Please input the buying price\n";
        while (!(in >> buyPrice)) // loop until we read an integer from the user.
        {  
            in.clear();
            cout<<"Invalid. Please input the buying price\n";
        }
        cout<<"Please input the selling price\n";
        while (!(in >> sellPrice))
        {
            in.clear();
            cout<<"Invalid. Please input the selling price\n";
        }
        return in;
    }
    // calc logic method goes here.
    // output method goes here.
private:
    string name;
    int buyPrice;  // no screwing around with the calc array. numbers are 
                   //read in as numbers or the input fails
    int sellPrice;
};

vector<Item> itemlistdec()
{
    int nrItems = 0;
    cout << "How many items would you like to input?";
    cin >> nrItems;
    vector<Item> itemlist(nrItems);
    for (Item& item : itemlist)
    {
        item.read(cin);
    }

    return itemlist;
}
类项目
{
公众:
无效读取(istream&in)
{
//忽略cin完全失效的可能性。
姓名;
cout buyPrice))//循环,直到从用户处读取整数。
{  
in.clear();

在我之前的注释中,你还应该了解C++中不存在的,所以像<代码>字符串项[4 ] [项目表]这样的声明
在技术上是无效的。呃……但奇怪的是,我的代码在这种状态下运行平稳?不管怎样,我肯定会读更多的书,我主修计算机科学,这只是我的第一门编码课程。:D感谢一些编译器确实支持可变长度数组(VLA)作为C++的扩展,但仅仅是因为他们选择这样做。你不能依赖某些C++工具接受VLAs。关于返回,它可能是没有向量等的,但是……如果你没有理由不使用它们(使用向量在C++中保证有可变大小)@ YouFaveRoo<代码>它附带DEV C++ +代码>是的,那就是GCC。(或Windows上的MinGW,基本上是Windows的GCC,只是名称不同)。关于您的讲师,没有
std::vector
的解决方案更复杂,涉及指针、动态分配(
new
)等等,他/她可能希望你练习……但是如果没有明确禁止……谢谢你的反馈,我很快就会看到链接……只是目前正在使用。前两个代码示例没有编译,即使你使用了常量数组维度。二维数组不能转换为指向指针的指针。@mattmcnab你的观点被接受了。我并没有一直关注这个问题。我要看看我能做些什么来澄清我试图用前两个词表达的观点,而不是把typedef混入其中。@MattMcNabb无法想出没有typedef的方法,所以我把它们放进去了。通过让一个糟糕的想法编译来改变我试图表达的观点。学得好不过是经验。
typedef string (array)[4];    
typedef array (*arrayPtr);    

arrayPtr itemlistdec()
{
    cout << "How many items would you like to input?";
    cin >> itemlist;

    arrayPtr item = new array[itemlist]; 
    //load the array

    return item;
}
string ** itemlistdec()
{
    cout << "How many items would you like to input?";
    cin >> itemlist;
    string** item = new string*[4]; // 4 can be any number, but you also have to 
                                    // use it in place of 4 in the for loop
    for (size_t index = 0; index < 4; index++)
    {
        item[index] = new string[itemlist];
    }
    //load the array

    return item;
}
vector<vector<string>> itemlistdec()
{
    int itemlist;
    cout << "How many items would you like to input?";
    cin >> itemlist;
    vector<vector<string>> item(4, vector<string>(itemlist));
    for (int c=0;c<itemlist;c++)
    {
        cout<<"Please enter the name of the item ";
        cin>>item[0][c];
        cout<<"Please input the buying price\n";
        cin>>item[1][c];
        cout<<"Please input the selling price\n";
        cin>>item[2][c];
    }

    return item;
}
class Item
{
public:
    void read(istream & in)
    {
        // ignoring possibility of total failure of cin here.
        cout<<"Please enter the name of the item ";
        in >> name;
        cout<<"Please input the buying price\n";
        while (!(in >> buyPrice)) // loop until we read an integer from the user.
        {  
            in.clear();
            cout<<"Invalid. Please input the buying price\n";
        }
        cout<<"Please input the selling price\n";
        while (!(in >> sellPrice))
        {
            in.clear();
            cout<<"Invalid. Please input the selling price\n";
        }
        return in;
    }
    // calc logic method goes here.
    // output method goes here.
private:
    string name;
    int buyPrice;  // no screwing around with the calc array. numbers are 
                   //read in as numbers or the input fails
    int sellPrice;
};

vector<Item> itemlistdec()
{
    int nrItems = 0;
    cout << "How many items would you like to input?";
    cin >> nrItems;
    vector<Item> itemlist(nrItems);
    for (Item& item : itemlist)
    {
        item.read(cin);
    }

    return itemlist;
}