我试图创建一个程序,从用户那里获取一个字符串,搜索一个2D数组,如果它匹配数组中的字符串,则打印整行。所以基本上,如果用户输入名称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语句并使其变得混乱的情况下这样做。
答案 0 :(得分:2)
我真的很难理解你在代码中写的内容,但根据描述,这应该类似于(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
检查地图中是否有这样的字符串作为键,如果它包含此键,则会返回1
。
但严重的是,你必须更认真地阅读;找到一本教程和/或一本书,直接理解你的观点。
答案 1 :(得分:0)
任务并不像看起来那么简单。所以我向你投了你的问题。
首先,最好使用类型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;
}
您可以引入一些typedef名称,而不是使用长类型名称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;
}
祝你好运。