首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++:循环中向量的问题

C++:循环中向量的问题
EN

Stack Overflow用户
提问于 2013-11-01 03:14:12
回答 4查看 668关注 0票数 1

CreateList函数必须: 1.向用户询问杂货店名称。2.要求用户为这家杂货店输入商品清单,直到用户输入“完成”为止。3.在输入字符串时将项目添加到字符串向量中。4.显示:“将物品添加到杂货店名称列表中。”在输入每个项目之后,其中“item”是输入的项目,而“杂货店名称”是在上面步骤1中输入的名称。

代码语言:javascript
复制
void CreateList()
{   
    string store;
    string item;
    int count = 0;
    cout<<"What is the grocery store name"<<endl;
    cin>>store;
    vector<string> store;
    cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;
    cin>>item;
    store[count] = item; //error saying no conversion?
    count++;
    cout<<"Added "<<item<<"to "<<store<<"list"<<endl;
    bool finished=true;
    while (finished = true)
    {
        cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;
        cin>>item;

        if (item == "done")
        break;

        store[count] = item; //error saying no conversion?
        count++;
        cout<<"Added "<<item<<"to "<<store<<"list"<<endl;
}


}

有几个关于我的函数的问题,不知道不转换错误是从哪里来的,这能在do while循环中实现吗?请尽量保持您的答案尽可能简单,这是我在C++中第一次尝试从python过渡。耽误您时间,实在对不起。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-11-01 03:18:19

你在里面有很多错误。首先,正如您所看到的,您正在重复大量代码两次。这表明您应该重构代码。你对do {} while()的直觉是正确的,你绝对应该使用它。

其次,您不会像在operator[]中那样,通过store[count]将新条目推入向量。您应该使用store.push_back(item)store.emplace_back(item)。向量是一个动态容器,因此一旦创建它就不包含任何元素。尝试使用store[0]访问第一个元素将导致未定义的行为,并且很可能是分段错误。

第三,正如您所看到的,您也没有真正使用这个finished变量(实际上,您是用finished = true分配它,而不是检查它是否为真,即finished == true)。因为您使用break正确地退出了循环。所以你一定要把它移走。

第四,将存储名命名为store,并使用相同的名称声明存储列表向量。对于同一代码块中的两种不同类型,您不应该使用相同的名称。

最后,我看到您使用的是类似于using namespace std;的东西。虽然这对于这类练习来说是可以的,但我认为最好习惯用std::作为标准库类的前缀,或者谨慎地“只包括”您真正需要的名称空间:

代码语言:javascript
复制
using std::string;
using std::cout;
// ...

这主要是因为“污染”了全局命名空间is considered bad practice,因为我不打算在这里讨论。

按照上面的指导方针,您可以得到类似于:

代码语言:javascript
复制
void CreateList() {  
    std::string store;
    std::string item;
    std::cout << "What is the grocery store name" << std::endl;
    std::cin >> store;

    std::vector<std::string> store_list;
    do {
        std::cout << "Enter a list of items for this grocery store one at a time. When you are done, type done." << std::endl;
        std::cin >> item;
        if (item == "done") break;
        store_list.emplace_back(item);
        std::cout << "Added " << item << "to " << store << "list" << std::endl;
    } while (true);
}
票数 2
EN

Stack Overflow用户

发布于 2013-11-01 03:29:43

你有两个变量叫做存储。一个是字符串,一个是字符串的向量。然后,您似乎混淆了这两者,将item分配给store[count] (它描述的是一个字符而不是字符串),然后尝试输出产品列表,因为它被认为是一个字符串。

使变量名变得合理应该修复您的代码:

代码语言:javascript
复制
void CreateList()
{   
    std::string storeName;
    std::cout << "What is the grocery store name" << std::endl;
    std::cin >> storeName;

    std::cout << "Enter a list of items for this grocery store one at a time. When you are done, type done." << std::endl;

    std::vector<std::string> products;
    while (true)
    {
        std::string product;
        std::cin >> product;

        if (product == "done")
            break;

        products.push_back(product);

        std::cout << "Added " << product << " to " << storeName << " list" << std::endl;
    }
}

有一个由C++继承的C编程伪码,它允许您用同名的不同变量“阴影”一个变量。

代码语言:javascript
复制
#include <iostream>

int i = 20;

int main() {
    int i = 10;
    for (int i = 0; i < 5; ++i) {
        std::cout << "loop i = " << i << std::endl;
    }
    std::cout << "i = " << i << std::endl;
}

现场演示:http://ideone.com/eKayzN

票数 0
EN

Stack Overflow用户

发布于 2013-11-01 03:30:11

首先,您有两个不同的循环要读取您的值,但两者或多或少都是坏的,应该是真正的一个循环。

另外,您在第二个循环中得到了一个赋值,并声明了两个不同的变量,名称为store,一个是string,另一个是vector<string>。您不能在同一范围内有两个同名的不同变量。

我认为你真正想做的是这样的事情:

代码语言:javascript
复制
vector<string> CreateList()
{   
    string store;
    string item;
    cout<<"What is the grocery store name"<<endl;
    cin>>store;
    vector<string> store_list;
    cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;

    while (cin>>item && item != "done") // read next item as long as input didn't fail and item isn't "done".
    {
        store_list.pushback(item); // using pushback for adding new item
        cout<<"Added "<< item <<"to "<< store << " list"<<endl;
    }

    return store_list

}

您可能还想以某种方式返回商店列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19720196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档