繁体   English   中英

为什么在 C++ 中迭代向量时无法进行比较?

[英]Why can't I perform a comparison when iterating over a vector in C++?

我正在尝试查看向量的每个元素,看看它是否等于指定的字符串。

代码如下所示:

for (int i = 0; i < menuVector.size(); i++) {
  if (menuVector[i] == "a") {
    type = "Appetisers";
    name = menuVector[i + 1];
    price = std::stod(menuVector[i + 2]);
    calories = std::stoi(menuVector[i + 3]);

    if (menuVector[i + 4] == "y") {
      shareable = true;
    } else {
      shareable = false;
    }

    if (menuVector[i + 5] == "y") {
      twoForOne = true;
    } else {
      twoForOne = false;
    }

    createMenu(shareable, twoForOne, name, price, calories, type, millilitres, abv);
  }
                
  if (menuVector[i] == "m") {
    type = "Main dishes";
    name = menuVector[i + 1];
    price = std::stod(menuVector[i + 2]);
    calories = std::stoi(menuVector[i + 3]);
    createMenu(shareable, twoForOne, name, price, calories, type, millilitres, abv);
  }

  if (menuVector[i] == "b") {
    type = "Beverages";
    name = menuVector[i + 1];
    price = std::stod(menuVector[i + 2]);
    calories = std::stoi(menuVector[i + 3]);
    millilitres = std::stoi(menuVector[i + 6]);
    abv = std::stod(menuVector[i + 7]);

    createMenu(shareable, twoForOne, name, price, calories, type, millilitres, abv);
  }
}

与“a”的第一次比较有效。 但是,对于“a”的其他实例,它无法再次工作。 与“m”和“b”的所有其他比较都不起作用。 尽管如果我写 'std::cout << menuVector[42];` 它会给我“m”。

使用相同for循环打印出数组中的每个元素给我这个 output:

aNachos4.99600yn
aBuffalo wings3.99450ny
aGarlic bread3.99500ny
mBurger9.99950
mMac & cheese7.99850
mFish & chips8.991000
mChicken tikka masala6.99700
bLager3.52005684.5
bWhite wine415017511.5
bRed wine417017512.5
bCoke2.51403300
bWater1.503300

显示菜单的 function 如下所示:

std::string Menu::createMenu(bool shareable, bool twoForOne, std::string name, double price, int calories, std::string type,int millilitres,double abv) {
  std::cout << "--------------------" << type << "-----------------------" << std::endl;
  std::cout << name << ": \x9C"; 
  std::printf("%.2f", (price));
  std::cout << ", " << std::to_string(calories) << " cal, ";

  if (type == "Beverages") {
    std::cout << "(" << millilitres << ", " << abv << " abv)";
  }

  if (shareable) {
    std::cout << "(shareable)" << std::endl;
  } else if(twoForOne) {
    std::cout << "(2-4-1)" << std::endl;
  }

  return name;
}

当前的output是这样的:

--------------------Appetisers-----------------------
Nachos: £4.99, 600 cal, (shareable)

为什么只输出一份开胃菜? 当然,当menuVector[i]等于该字符串时,它应该输入每个if语句。

这是一个最小的、可重现的示例:

菜单.h 文件:

#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cmath>
#include <iomanip>

class Menu 
{
private:
    std::vector<std::string> menuVector;
public:
    Menu(std::string menuDirectory);
    std::string createMenu(bool shareable, bool twoForOne, std::string name, double price, int calories, std::string type, int millilitres, double abv);


    

};

菜单.cpp 文件:

#include "Menu.h"
Menu::Menu(std::string menuDirectory) {

    std::ifstream menuFile;

    bool shareable{ false };
    bool twoForOne{ false };
    std::string name{ " " };
    double price{ 0 };
    int calories{ 0 };
    std::string type{ " " };
    int millilitres{ 0 };
    double abv{ 0 };


    while (true) {

        try {
            menuFile.open(menuDirectory);
            std::string line;


            std::vector<std::string> menuVector;

            while (std::getline(menuFile, line, ',')) {


                menuVector.push_back(line);

            }



            for (int i = 0; i < menuVector.size(); i++) {




                if (menuVector[i] == "a") {


                    type = "Appetisers";

                    name = menuVector[i + 1];
                    price = std::stod(menuVector[i + 2]);
                    calories = std::stoi(menuVector[i + 3]);
                    if (menuVector[i + 4] == "y") {
                        shareable = true;
                    }
                    else {
                        shareable = false;
                    }

                    if (menuVector[i + 5] == "y") {
                        twoForOne = true;
                    }
                    else {
                        twoForOne = false;
                    }

                    createMenu(shareable, twoForOne, name, price, calories, type, millilitres, abv);

                }


                if (menuVector[i] == "m") {
                    type = "Main dishes";

                    name = menuVector[i + 1];
                    price = std::stod(menuVector[i + 2]);
                    calories = std::stoi(menuVector[i + 3]);
                    createMenu(shareable, twoForOne, name, price, calories, type, millilitres, abv);
                }


                if (menuVector[i] == "b") {
                    type = "Beverages";
                    name = menuVector[i + 1];
                    price = std::stod(menuVector[i + 2]);
                    calories = std::stoi(menuVector[i + 3]);
                    millilitres = std::stoi(menuVector[i + 6]);
                    abv = std::stod(menuVector[i + 7]);

                    createMenu(shareable, twoForOne, name, price, calories, type, millilitres, abv);

                }


            }




            


        }
        catch (const std::ifstream::failure& e) {
            std::cout << "An error occured reading the menu.csv file" << std::endl;
        }

        break;

    }


    menuFile.close();

}

std::string Menu::createMenu(bool shareable, bool twoForOne, std::string name, double price, int calories, std::string type, int millilitres, double abv) {


    std::cout << "--------------------" << type << "-----------------------" << std::endl;
    std::cout << name << ": \x9C";
    std::printf("%.2f", (price));
    std::cout << ", "
        << std::to_string(calories) << " cal, ";

    if (type == "Beverages") {
        std::cout << "(" << millilitres << ", "
            << abv << " abv)";
    }

    if (shareable) { std::cout << "(shareable)" << std::endl; }
    else if (twoForOne) {
        std::cout << "(2-4-1)" << std::endl;
    }

    return name;
}

Main.cpp 文件:

#include "Menu.h"


int main()
{
    Menu menu = Menu("menu.csv");
}

读入向量的 CSV 文件数据

a,Nachos,4.99,600,y,n,,
a,Buffalo wings,3.99,450,n,y,,
a,Garlic bread,3.99,500,n,y,,
m,Burger,9.99,950,,,,
m,Mac & cheese,7.99,850,,,,
m,Fish & chips,8.99,1000,,,,
m,Chicken tikka masala,6.99,700,,,,
b,Lager,3.5,200,,,568,4.5
b,White wine,4,150,,,175,11.5
b,Red wine,4,170,,,175,12.5
b,Coke,2.5,140,,,330,0
b,Water,1.5,0,,,330,0

您的 csv 读数有问题。 通过拆分,字符, \n会附加到下一个标记上,例如 csv 中的下a它是\na

以此为例

#include <string>
#include <iostream>
#include <sstream>

int main() {
    std::stringstream in;
    in << "1,2,3\n4,5,6\n7,8,9\n";
    std::string line;
    while (std::getline(in, line, ','))
        std::cout << line;
}

output 是

123
456
789

您应该按行拆分,然后按令牌拆分

#include <string>
#include <iostream>
#include <sstream>

int main() {
    std::stringstream in;
    in << "1,2,3\n4,5,6\n7,8,9\n";
    std::string line, token;
    while (std::getline(in, line, '\n')) {
        std::stringstream liness;
        liness << line;
        while (std::getline(liness, token, ','))
            std::cout << token;
    }
}

output 是

123456789

暂无
暂无

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

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