c++ - 我如何创建一个函数(),其中一个参数是映射
问题描述
#include <iostream>
#include <string>
#include <map>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
void print( string::size_type n, string const & s )
{
if( n == string::npos ) {
std::cout << "not found\n";
} else {
cout << "found: " << s.substr( n ) << '\n';
}
}
//////I WANT HERE A FUNCTION WITH ONE PAREMETR - MAP///////
int main()
{
map < char, int > pythagorean, simpleeng, gfive;
pythagorean[ 'A' ] = 1;
pythagorean[ 'a' ] = 1;
pythagorean[ 'B' ] = 2;
pythagorean[ 'b' ] = 2;
pythagorean[ 'C' ] = 3;
pythagorean[ 'c' ] = 3;
pythagorean[ 'D' ] = 4;
pythagorean[ 'd' ] = 4;
pythagorean[ 'E' ] = 5;
pythagorean[ 'e' ] = 5;
pythagorean[ 'F' ] = 6;
pythagorean[ 'f' ] = 6;
pythagorean[ 'G' ] = 7;
pythagorean[ 'g' ] = 7;
pythagorean[ 'H' ] = 8;
pythagorean[ 'h' ] = 8;
pythagorean[ 'I' ] = 9;
pythagorean[ 'i' ] = 9;
pythagorean[ 'J' ] = 1;
pythagorean[ 'j' ] = 1;
pythagorean[ 'K' ] = 2;
pythagorean[ 'k' ] = 2;
pythagorean[ 'L' ] = 3;
pythagorean[ 'l' ] = 3;
pythagorean[ 'M' ] = 4;
pythagorean[ 'm' ] = 4;
pythagorean[ 'N' ] = 5;
pythagorean[ 'n' ] = 5;
pythagorean[ 'O' ] = 6;
pythagorean[ 'o' ] = 6;
pythagorean[ 'P' ] = 7;
pythagorean[ 'p' ] = 7;
pythagorean[ 'Q' ] = 8;
pythagorean[ 'q' ] = 8;
pythagorean[ 'R' ] = 9;
pythagorean[ 'r' ] = 9;
pythagorean[ 'S' ] = 1;
pythagorean[ 's' ] = 1;
pythagorean[ 'T' ] = 2;
pythagorean[ 't' ] = 2;
pythagorean[ 'U' ] = 3;
pythagorean[ 'u' ] = 3;
pythagorean[ 'V' ] = 4;
pythagorean[ 'v' ] = 4;
pythagorean[ 'W' ] = 5;
pythagorean[ 'w' ] = 5;
pythagorean[ 'X' ] = 6;
pythagorean[ 'x' ] = 6;
pythagorean[ 'Y' ] = 7;
pythagorean[ 'y' ] = 7;
pythagorean[ 'Z' ] = 8;
pythagorean[ 'z' ] = 8;
simpleeng[ 'A' ] = 1;
simpleeng[ 'a' ] = 1;
simpleeng[ 'B' ] = 2;
simpleeng[ 'b' ] = 2;
simpleeng[ 'C' ] = 3;
simpleeng[ 'c' ] = 3;
simpleeng[ 'D' ] = 4;
simpleeng[ 'd' ] = 4;
simpleeng[ 'E' ] = 5;
simpleeng[ 'e' ] = 5;
simpleeng[ 'F' ] = 6;
simpleeng[ 'f' ] = 6;
simpleeng[ 'G' ] = 7;
simpleeng[ 'g' ] = 7;
simpleeng[ 'H' ] = 8;
simpleeng[ 'h' ] = 8;
simpleeng[ 'I' ] = 9;
simpleeng[ 'i' ] = 9;
simpleeng[ 'J' ] = 1;
simpleeng[ 'j' ] = 1;
simpleeng[ 'K' ] = 2;
simpleeng[ 'k' ] = 2;
simpleeng[ 'L' ] = 3;
simpleeng[ 'l' ] = 3;
simpleeng[ 'M' ] = 4;
simpleeng[ 'm' ] = 4;
simpleeng[ 'N' ] = 5;
simpleeng[ 'n' ] = 5;
simpleeng[ 'O' ] = 6;
simpleeng[ 'o' ] = 6;
simpleeng[ 'P' ] = 7;
simpleeng[ 'p' ] = 7;
simpleeng[ 'Q' ] = 8;
simpleeng[ 'q' ] = 8;
simpleeng[ 'R' ] = 9;
simpleeng[ 'r' ] = 9;
simpleeng[ 'S' ] = 1;
simpleeng[ 's' ] = 1;
simpleeng[ 'T' ] = 2;
simpleeng[ 't' ] = 2;
simpleeng[ 'U' ] = 3;
simpleeng[ 'u' ] = 3;
simpleeng[ 'V' ] = 4;
simpleeng[ 'v' ] = 4;
simpleeng[ 'W' ] = 5;
simpleeng[ 'w' ] = 5;
simpleeng[ 'X' ] = 6;
simpleeng[ 'x' ] = 6;
simpleeng[ 'Y' ] = 7;
simpleeng[ 'y' ] = 7;
simpleeng[ 'Z' ] = 8;
simpleeng[ 'z' ] = 8;
gfive[ 'A' ] = 7;
gfive[ 'a' ] = 7;
gfive[ 'B' ] = 8;
gfive[ 'b' ] = 8;
gfive[ 'C' ] = 1;
gfive[ 'c' ] = 1;
gfive[ 'D' ] = 2;
gfive[ 'd' ] = 2;
gfive[ 'E' ] = 3;
gfive[ 'e' ] = 3;
gfive[ 'F' ] = 4;
gfive[ 'f' ] = 4;
gfive[ 'G' ] = 5;
gfive[ 'g' ] = 5;
gfive[ 'H' ] = 6;
gfive[ 'h' ] = 6;
gfive[ 'I' ] = 7;
gfive[ 'i' ] = 7;
gfive[ 'J' ] = 8;
gfive[ 'j' ] = 8;
gfive[ 'K' ] = 9;
gfive[ 'k' ] = 9;
gfive[ 'L' ] = 1;
gfive[ 'l' ] = 1;
gfive[ 'M' ] = 2;
gfive[ 'm' ] = 2;
gfive[ 'N' ] = 3;
gfive[ 'n' ] = 3;
gfive[ 'O' ] = 4;
gfive[ 'o' ] = 4;
gfive[ 'P' ] = 5;
gfive[ 'p' ] = 5;
gfive[ 'Q' ] = 6;
gfive[ 'q' ] = 6;
gfive[ 'R' ] = 7;
gfive[ 'r' ] = 7;
gfive[ 'S' ] = 8;
gfive[ 's' ] = 8;
gfive[ 'T' ] = 9;
gfive[ 't' ] = 9;
gfive[ 'U' ] = 1;
gfive[ 'u' ] = 1;
gfive[ 'V' ] = 2;
gfive[ 'v' ] = 2;
gfive[ 'W' ] = 3;
gfive[ 'w' ] = 3;
gfive[ 'X' ] = 4;
gfive[ 'x' ] = 4;
gfive[ 'Y' ] = 5;
gfive[ 'y' ] = 5;
gfive[ 'Z' ] = 6;
gfive[ 'z' ] = 6;
int cho = 0, d;
char o;
string phrasetwo;
while(1)
{
system( "cls" );
cout << "You want to use: " << endl << "1) Pythagorean Gematria" << endl << "2) English/Simple Gematria" << endl << "3) Extra: " << endl << "Gematria with G = 7" << endl << endl;
cin >> phrasetwo;
istringstream asd( phrasetwo );
if ( !( asd >> d ) || asd >> o )
{
system( "cls" );
cout << "You want to use: " << endl << "1) Pythagorean Gematria" << endl << "2) English/Simple Gematria" << endl << "3) Extra: " << endl << "Gematria with G = 7" << endl << endl;
}
else
{
cho = stoi( phrasetwo );
if( cho <= 3 && cho >= 1 )
{
break;
}
}
}
switch( cho )
{
case 1:
{
cout << "You chose Pythagorean MAP" << endl;
break;
}
case 2:
{
cout << "You chose Simpleeng MAP" << endl;
break;
}
case 3:
{
cout << "You chose Gfive MAP" << endl;
break;
}
}
///////EVERYTHING DOWN SHOULD BE ADDED INTO FUNCTION/////////////
string::size_type n;
ifstream input( "words.txt" );
ofstream output( "results.txt" );
string str, phrase;
int counter = 0, cnt = 0, choice = 0, chosennum = 0, asd = 0, x;
bool flag;
char c;
while(1)
{
system( "cls" );
cout << "You want to check:" << '\n' << "1) Number" << '\n' << "2) Phrase" << endl;
cin >> phrase;
istringstream s( phrase );
if ( !( s >> x ) || s >> c )
{
system( "cls" );
cout << "You want to check:" << '\n' << "1) Number" << '\n' << "2) Phrase" << endl;
}
else
{
choice = stoi( phrase );
if( choice <= 2 && choice >= 1 )
{
break;
}
}
}
switch( choice )
{
case 1:
{
cout << "Which number do you want to check?: ";
while(1)
{
cin >> phrase;
istringstream s( phrase );
if ( !( s >> x ) || s >> c )
{
cout << endl << "Which number do you want to check?: ";
}
else
{
chosennum = stoi( phrase );
break;
}
}
cout << endl << "All words found: " << endl;
break;
}
case 2:
{
do
{
flag = false;
cout << "Which phrase do you want to check?: ";
cin >> phrase;
for (int i = 0; i < phrase.length(); i++)
{
if ( isdigit( phrase[ i ] ) )
{
flag = true;
}
}
cout << endl;
} while (flag == true);
for( char charr: phrase )
{
chosennum += pythagorean[ charr ];
}
cout << '\t' << phrase << " in Pythagorean Gematria = " << chosennum << endl << endl << "All words found: " << endl;
break;
}
}
while( getline( input, str ) )
{
int sum = 0;
n = str.find( ',' );
if (n != string::npos)
{
str.resize( n );
}
for( char charr: str )
{
sum += pythagorean[ charr ];
}
if( sum == chosennum )
{
cout << '\t' << "Pythagorean: " << str << "(" << sum << ")" << '\n';
output << "Pythagorean: " << '\t' << str << " = " << sum << endl;
counter++;
}
}
cout << "\n\n You have found: " << counter << " words" << '\n';
output << "\n\n You have found: " << counter << " words" << '\n';
return 0;
}
如您所见,我已经声明了 3 个不同的地图pythagorean
、simpleeng
和gfive
。它们对字符有不同的值,我想创建一个函数来计算这个循环中的总和:
for( char charr: str )
{
sum += pythagorean[ charr ];
}
以 3 种不同的方式,通过使用pythagorean
、simpleeng
和gfive
映射。
例如,我运行程序,我必须选择 1-3 选项:
Pythagorean Gematria, Simple/English, Gematria with G = 7
如果我选择数字 1,程序将使用pythagorean
地图计算所有内容,依此类推。
我正在寻求帮助,因为看起来我做错了什么,我真的需要这个。
int cho = 0, d;
char o;
string phrasetwo;
while(1)
{
system( "cls" );
cout << "You want to use: " << endl << "1) Pythagorean Gematria" << endl << "2) English/Simple Gematria" << endl << "3) Extra: " << endl << "Gematria with G = 7" << endl << endl;
cin >> phrasetwo;
istringstream asd( phrasetwo );
if ( !( asd >> d ) || asd >> o )
{
system( "cls" );
cout << "You want to use: " << endl << "1) Pythagorean Gematria" << endl << "2) English/Simple Gematria" << endl << "3) Extra: " << endl << "Gematria with G = 7" << endl << endl;
}
else
{
cho = stoi( phrasetwo );
if( cho <= 3 && cho >= 1 )
{
break;
}
}
}
switch( cho )
{
case 1:
{
cout << "You chose Pythagorean MAP" << endl; ///HERE I WANT SUM FROM USING PYTHAGOREAN MAP/// LIKE FUNCTION ASD(PYTHAGOREAN MAP)
break;
}
case 2:
{
cout << "You chose Simpleeng MAP" << endl; ///HERE I WANT SUM FROM USING SIMPLEENG MAP/// LIKE FUNCTION ASD(SIMPLEENG MAP)
break;
}
case 3:
{
cout << "You chose Gfive MAP" << endl; ///HERE I WANT SUM FROM USING GFIVE MAP/// LIKE FUNCTION ASD(GFIVE MAP)
break;
}
}
解决方案
您可以像创建任何其他作为参数中的变量的函数一样执行此操作。IE
#include <iostream>
#include <numeric>
#include <map>
int get_map_sum(std::map<char, int>& my_map) {
return std::accumulate(my_map.begin(), my_map.end(), 0, [](int acc, const std::map<char, int>::value_type& p){ return acc + p.second; });
}
int main() {
std::map<char, int> pythagorean = {
{'A', 1},
{'a', 1},
{'B', 2},
{'b', 2},
// ... So on and so forth
};
std::cout << get_map_sum(pythagorean) << std::endl;
// Output is 6, as expected.
}
我建议使用我使用过的地图的初始化格式,因为它的输入更少并且更易于理解。
推荐阅读
- python - 从 pd.Series 列表中过滤掉 None / 或在 pd.Series 列表中找出 Nonetype 对象
- sql-server-2012 - SQL Server:如何选择两列的多对多值
- html - 为什么子div不会增加父div的高度?
- javascript - 设置后范围在Angularjs中未定义
- python - 连接 Panda Dataframe 中的一些列?
- c++ - 使用多线程将随机字符串写入全局指针
- unix - lsyncd 包含特定文件 排除其余文件
- javascript - 试图在第一次响应之前阻止第二次点击
- ruby - 安装 Ruby 和 GCC 时出现“E: Unable to locate package ruby and E: Package 'texinfo' has no installation Candidate”
- node.js - 从 NodeJS 8.x 更新到 NodeJS 14.x 后,错误的密码验证有效