获取用户输入并搜索2D数组

Take user input and search through a 2D array

本文关键字:2D 数组 搜索 用户 输入 获取      更新时间:2023-10-16

我试图制作一个程序,从用户接收字符串,通过二维数组搜索,如果它匹配数组中的字符串,打印整个行。所以基本上,如果用户输入名称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;
}

好运。