【问题标题】:this vector has vector probelem is not working这个向量有向量问题不工作
【发布时间】:2022-01-24 01:26:56
【问题描述】:

问题在于它会打印全名,但不会打印有关此人的其余行。 有人可以指导我吗? 非常感谢您的帮助!









  auto itr = find(my_vec.begin(), my_vec.end(), search );
   if(itr != my_vec.end()) 
   {  
    std::cout << "Match found " <<  search << std::endl;
    std::cout <<  "\nFull name:    " << search << std::endl;
       } else {
         std::cout << "Match not found "<< std::endl;
       }






【问题讨论】:

  • 如果我给你一个行列表,作为一个人,你如何知道某个人的记录何时结束而另一个人的记录何时开始?
  • 作为新用户,请阅读tourHow to Askminimal reproducible example。这个问题有很多额外的信息,使它更难回答。好问题需要时间来写,但他们得到答案的速度要快得多(或根本没有)。
  • 好吧,我想,我会查找列表中的行。检查它是开始还是结束。 (=
  • 嘿约翰!谢谢!我会读的! (=
  • 记录的结尾真的以&lt;=== Line about the person ends here! 结尾吗?如果是这样,并且如果这就是作为人类的你会识别记录结尾的方式,那么你应该让计算机这样做。

标签: visual-c++


【解决方案1】:

您的代码存在一些样式问题:

  1. 无需显式初始化字符串,默认为空(参见here)。
  2. 保持一致的风格。例如,要么总是在函数签名的同一行或下一行开始括号。
  3. 无需在函数结束时显式关闭文件,这是在对象超出范围时完成的(请参阅(destructor)here)。
  4. 无需包含&lt;map&gt;&lt;iomanip&gt; 标头。
  5. 不要保留未使用的变量。
  6. 为变量提供暗示性名称。
  7. 当应用程序正常运行时,不要向操作系统返回错误代码。找不到名字不是错误,是吗?

您的文件似乎每个联系人有 6 个条目,因此您只需再打印 5 行即可。您不需要将这些行存储在向量中,只需随时解析和打印它们。这是一个例子:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>

void findContact(std::string fullName, std::string contactListPath) {

    std::ifstream inFile{contactListPath};
    if (!inFile) {
        std::cerr << "File could not be open!" << std::endl;
        return;
    }

    std::string line;
    while (std::getline(inFile, line)) {
        if (line == fullName) {
            std::cout << "Match found: \n";
            std::cout << "\nFull name: " << fullName;
            std::cout << "\nAddress: " << (std::getline(inFile, line), line);
            std::cout << "\nE-mail: " << (std::getline(inFile, line), line);
            std::cout << "\nPhone: " << (std::getline(inFile, line), line);
            std::cout << "\nBirthday: " << (std::getline(inFile, line), line);
            std::cout << "\nNote: " << (std::getline(inFile, line), line) << std::endl;
            return;
        }
    }

    std::cout << "Match not found " << std::endl;
}

int main() {

    std::string fullName;
    std::string contactListPath;

    std::cout << "Enter full name to search: ";
    std::getline(std::cin, fullName);

    std::cout << "Enter path to contact list: ";
    std::getline(std::cin, contactListPath);

    findContact(fullName, contactListPath);

    return 0;
}

【讨论】:

  • 嘿漫画!非常感谢您的回答!我发自内心的感激!
  • 我怎样才能像你一样学习编程?
  • @Justin 是的,抱歉,我忽略了order of evaluation 问题。在我的机器上它打印得很好,但是不能保证操作的顺序,这就是为什么它不能很好地打印出来。插入运算符 (
  • @Justin 那是逗号操作符。从左到右计算表达式,并返回最右边表达式的值。所以,这就是我发现从文件中读取一行并在同一个语句中返回它的方式。 tutorialspoint.com/cplusplus/cpp_comma_operator.htm
  • @Justin 我进入 CSE 已经 10 年了,我自己还有很多东西要学。我能给你的建议是学习和实践。在线阅读教程,观看 YouTube 视频,对一切充满好奇。开始你自己的爱好项目。他们一开始会很笨拙,但会让你从经验中学习。请一位资深人士检查您的代码。即使是小事也不要害怕提出问题。祝你好运。 :)
【解决方案2】:

如果每个条目包含 6 行。然后您可以打印从您找到的行开始的所有行:

auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end()) 
{  
  std::cout << "Match found " << std::endl;
  // print the next 6 lines
  for(int remaining = 6;remaining > 0 && itr!=my_vec.end(); itr++,remaining--) {
    std::cout << *itr << std::endl;
  }
} else {
      std::cout << "Match not found "<< std::endl;
}

【讨论】:

  • 嘿彼得!非常感谢你的帮助!我明白我做错了什么!
  • 我的意思是,我失踪了!我以前不知道这种方法!你能告诉我如何学习这种数据操作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多