获取用户输入并搜索2D数组
Take user input and search through a 2D array
我试图制作一个程序,从用户接收字符串,通过二维数组搜索,如果它匹配数组中的字符串,打印整个行。所以基本上,如果用户输入名称Bobby G
,我希望它输出Bobby G: ugly and stupid
,如果输入是Billy
,它输出Billy: bad
,以此类推。下面是我到目前为止所做的。如果你能解释一下,我将不胜感激。
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
//!! It only works with letters of the alphabet, so if I replaced Bobby G with the
//letter "A" It would output "ugly, and stupid", and then replace "Billy Smalls" with
//the letter "B" I'd get "Bad" so on and so forth, but I need it to work with the exact
// string, so user input of "Bobby G" outputs "ugly, and stupid"
std::string name[9][2] = {
{"Bobby G","ugly, and stupid"},
{"Billy","bad"},
{"John","smart and cool"},
{"Adam","amzing and beautiful"},
{"Bill","perfect"},
{"Turner","funny"},
{"Sonny","nice"},
{"Jack","radical"},
{"Frank","nice"}};
typedef std::string Full[2];
Full* last_Full = name + sizeof(name) / sizeof(Full);
struct Less {
bool operator () (const Full& a, const string& b) const
{
return a[0] < b;
}
};
std::string input;
std::cin >> input;
Less less_full;
Full* full = std::lower_bound(name, last_Full, input, less_full);
if(full == last_Full || (*full)[0] != input)
std::cout << "Not found" << std::endl;
else std::cout << (*full)[0] << ": " << (*full)[1] << std::endl;
system("Pause");
return 0;
}
我希望这样做,而不是嵌套if语句,使它变得一团糟。
我真的很难理解你写的代码,但根据描述,这应该是这样的(demo):
#include <iostream>
#include <unordered_map>
#include <string>
int main(void) {
std::unordered_map<std::string, std::string> name = {
{"Bobby G","ugly, and stupid"},
{"Billy","bad"},
{"John","smart and cool"},
{"Adam","amzing and beautiful"},
{"Bill","perfect"},
{"Turner","funny"},
{"Sonny","nice"},
{"Jack","radical"},
{"Frank","nice"}};
std::string in;
std::getline(std::cin,in);
if(name.count(in)){
std::cout << in << " " << name[in] << std::endl;
}
return 0;
}
为了避免我们在未来的头痛缩进你的代码,不要让它看起来像ASCII艺术…
所以基本上我们使用的是一个unordered_map,它把名字作为键,把句子作为值。
然后使用cin接收来自用户的输入,并将其放入string in
。
最后一步是使用count
检查map中是否有这样一个字符串作为键,如果它包含该键,则返回1
。
但是说真的,你必须更认真地阅读;找一本教程和/或一本书,把你的概念弄清楚。
这项任务并不像看上去那么简单。所以我给你的问题投了赞成票。
首先,最好使用std::pair<std::string, std::string>
类型的一维数组,而不是您使用的二维数组。其次,要应用std::lower_bound
算法,必须对数组进行排序。
代码可以如下所示
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <utility>
#include <cctype>
int main()
{
std::pair<std::string, std::string> name[] =
{
{ "Bobby G", "ugly, and stupid" },
{ "Billy", "bad" },
{ "John", "smart and cool" },
{ "Adam", "amzing and beautiful" },
{ "Bill", "perfect" },
{ "Turner", "funny" },
{ "Sonny", "nice" },
{ "Jack", "radical" },
{ "Frank", "nice" }
};
std::sort( std::begin( name ), std::end( name ) );
auto compare_by_name =
[]( const std::pair<std::string, std::string> &p1,
const std::pair<std::string, std::string> &p2 )
{
return std::lexicographical_compare(
p1.first.begin(), p1.first.end(),
p2.first.begin(), p2.first.end(),
[]( char c1, char c2 )
{ return std::toupper( c1 ) < std::toupper( c2 ); } );
};
auto p = std::make_pair( std::string( "bobby g" ), std::string( "" ) );
auto it = std::equal_range( std::begin( name ), std::end( name ), p,
compare_by_name );
if ( it.first != it.second )
{
std::cout << it.first->first + ' ' + it.first->second << std::endl;
}
return 0;
}
输出为
Bobby G ugly, and stupid
是关于那些对你的问题投反对票的参与者:)
如果编译器相对于数组初始化项发出错误,则应按以下方式替换它们。例如
std::pair<std::string, std::string>( "Bobby G", "ugly, and stupid" ),
下面的代码使用了其他类型的初始化式
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <utility>
#include <cctype>
int main()
{
std::pair<std::string, std::string> name[] =
{
std::pair<std::string, std::string>( "Bobby G", "ugly, and stupid" ),
std::pair<std::string, std::string>( "Billy", "bad" ),
std::pair<std::string, std::string>( "John", "smart and cool" ),
std::pair<std::string, std::string>( "Adam", "amzing and beautiful" ),
std::pair<std::string, std::string>( "Bill", "perfect" ),
std::pair<std::string, std::string>( "Turner", "funny" ),
std::pair<std::string, std::string>( "Sonny", "nice" ),
std::pair<std::string, std::string>( "Jack", "radical" ),
std::pair<std::string, std::string>( "Frank", "nice" )
};
std::sort( std::begin( name ), std::end( name ) );
auto compare_by_name =
[]( const std::pair<std::string, std::string> &p1,
const std::pair<std::string, std::string> &p2 )
{
return std::lexicographical_compare(
p1.first.begin(), p1.first.end(),
p2.first.begin(), p2.first.end(),
[]( char c1, char c2 )
{ return std::toupper( c1 ) < std::toupper( c2 ); } );
};
auto p = std::make_pair( std::string( "bobby g" ), std::string( "" ) );
auto it = std::equal_range( std::begin( name ), std::end( name ), p,
compare_by_name );
if ( it.first != it.second )
{
std::cout << it.first->first + ' ' + it.first->second << std::endl;
}
return 0;
}
不使用长类型名称std::pair<std::string, std::string>
,您可以引入一些类型定义名称,例如
typedef std::pair<std::string, std::string> Pair;
下面是要求用户输入名称
的程序#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <utility>
#include <cctype>
int main()
{
std::pair<std::string, std::string> name[] =
{
std::pair<std::string, std::string>( "Bobby G", "ugly, and stupid" ),
std::pair<std::string, std::string>( "Billy", "bad" ),
std::pair<std::string, std::string>( "John", "smart and cool" ),
std::pair<std::string, std::string>( "Adam", "amzing and beautiful" ),
std::pair<std::string, std::string>( "Bill", "perfect" ),
std::pair<std::string, std::string>( "Turner", "funny" ),
std::pair<std::string, std::string>( "Sonny", "nice" ),
std::pair<std::string, std::string>( "Jack", "radical" ),
std::pair<std::string, std::string>( "Frank", "nice" )
};
std::sort( std::begin( name ), std::end( name ) );
auto compare_by_name =
[]( const std::pair<std::string, std::string> &p1,
const std::pair<std::string, std::string> &p2 )
{
return std::lexicographical_compare(
p1.first.begin(), p1.first.end(),
p2.first.begin(), p2.first.end(),
[]( char c1, char c2 )
{ return std::toupper( c1 ) < std::toupper( c2 ); } );
};
while ( true )
{
std::cout << "Enter name: ";
std:: string s;
std::getline( std::cin, s );
if ( s.empty() ) break;
auto p = std::make_pair( s, std::string( "" ) );
auto it = std::equal_range( std::begin( name ), std::end( name ), p,
compare_by_name );
if ( it.first != it.second )
{
std::cout << it.first->first + ' ' + it.first->second << std::endl;
}
else
{
std::cout << "The name is not found" << std::endl;
}
}
return 0;
}
好运。
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 获取 2D 数组 c++ 中的所有数字对
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 如何创建一个所有行大小不同的 2D 数组,并且用户将指定每行将有多少个元素?
- 使用指针算法修改函数中的 2D 数组
- 在C++中初始化和打印 2D 数组
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- C++ 2D 数组写入多个位置
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 如何从initializer_list初始化 2D 数组
- 交换函数不是在 C++ 中交换 2D 数组的元素