字符串比较向量 C++
Vector of strings comparison C++
我正在编写有关输入字符串的代码,然后将它们与向量中的其他元素进行比较,如果存在正匹配,则不要将它们放入。我写了这个:
// NamePair.cpp : definisce il punto di ingresso dell'applicazione console.
//
#include "stdafx.h"
#include "std_lib_facilities.h"
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> names;
vector<int> scores;
string name = "0";
int score = 0;
int error = 0;
int n = 0;
cout << "Type a name and a score: " << endl;
while (cin >> name >> score) {
++n;
cout << "This is # " << n << " name you typed." << endl;
if (n >= 2) {
for (int i : scores) {
if (names[i] == name) {
cout << "You have already typed this name dude!" << endl;
}
else if (name != "NoName") {
names.push_back(name);
scores.push_back(score);
}
else {
break;
}
}
}
}
for (int i = 0; i < scores.size(); ++i) {
cout << names[i] << "\t" << scores[i] << endl;
}
keep_window_open();
return 0;
}
问题是,当我尝试 运行 该程序时,它可以运行,但它似乎卡在了我不断添加姓名和分数的位置,但它显然没有做任何事情(既不显示如果键入 "NoName" 字符串,则警告消息也不会停止)。我不知道为什么!我已经尝试重写所有内容,但结果相同...
感谢帮助!
你检查向量中是否存在名称是错误的。变化
if (names[i] == name) {
至
if ((std::find(names.begin(), names.end(), name) != names.end()) {
此外,for (int i : scores)
循环似乎在这里是不必要的。
一个std::map
最适合这里。此代码段将帮助您
#include <bits/stdc++.h>
using namespace std;
int main() {
map<string, int> data;
string name;
int score;
for (int n = 0; cin >> name >> score; ++n) {
if (name != "NoName" || !data.count(name))
data[name] = score;
}
for (auto & i : data)
cout << i.first << " " << i.second << endl;
return 0;
}
您的问题出在您的 for 循环中。
您尝试在迭代向量的循环内将新元素推送到向量中。向量一开始是空的,所以程序永远不会进入循环,你也永远不会真正将任何元素压入向量。
我正在编写有关输入字符串的代码,然后将它们与向量中的其他元素进行比较,如果存在正匹配,则不要将它们放入。我写了这个:
// NamePair.cpp : definisce il punto di ingresso dell'applicazione console.
//
#include "stdafx.h"
#include "std_lib_facilities.h"
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> names;
vector<int> scores;
string name = "0";
int score = 0;
int error = 0;
int n = 0;
cout << "Type a name and a score: " << endl;
while (cin >> name >> score) {
++n;
cout << "This is # " << n << " name you typed." << endl;
if (n >= 2) {
for (int i : scores) {
if (names[i] == name) {
cout << "You have already typed this name dude!" << endl;
}
else if (name != "NoName") {
names.push_back(name);
scores.push_back(score);
}
else {
break;
}
}
}
}
for (int i = 0; i < scores.size(); ++i) {
cout << names[i] << "\t" << scores[i] << endl;
}
keep_window_open();
return 0;
}
问题是,当我尝试 运行 该程序时,它可以运行,但它似乎卡在了我不断添加姓名和分数的位置,但它显然没有做任何事情(既不显示如果键入 "NoName" 字符串,则警告消息也不会停止)。我不知道为什么!我已经尝试重写所有内容,但结果相同...
感谢帮助!
你检查向量中是否存在名称是错误的。变化
if (names[i] == name) {
至
if ((std::find(names.begin(), names.end(), name) != names.end()) {
此外,for (int i : scores)
循环似乎在这里是不必要的。
一个std::map
最适合这里。此代码段将帮助您
#include <bits/stdc++.h>
using namespace std;
int main() {
map<string, int> data;
string name;
int score;
for (int n = 0; cin >> name >> score; ++n) {
if (name != "NoName" || !data.count(name))
data[name] = score;
}
for (auto & i : data)
cout << i.first << " " << i.second << endl;
return 0;
}
您的问题出在您的 for 循环中。
您尝试在迭代向量的循环内将新元素推送到向量中。向量一开始是空的,所以程序永远不会进入循环,你也永远不会真正将任何元素压入向量。