需要一些帮助。我有一个我为项目编写的代码。我对如何将代码分成单独的.h和.cpp文件感到困惑。大多数搜索都使用了一个类。我没有参加这个课程。我只使用地图和矢量。我不完全确定怎么做,我也想知道怎么做!
代码是:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
map<int, string> Broncos_Roster;
Broncos_Roster[88] = " Demaryius Thomas, Georgia Tech, WR ";
Broncos_Roster[72] = " Garret Bolles , Utah, LT ";
Broncos_Roster[76] = " Max Garcia , Florida, LG ";
Broncos_Roster[61] = " Matt Paradis, Bosie State, C ";
Broncos_Roster[65] = " Ron Leary, Memphis, RG ";
Broncos_Roster[75] = " Menelik Watson, Florida State, RT ";
Broncos_Roster[85] = " Virgil Green, Nevada, TE ";
Broncos_Roster[10] = " Emmanuel Sanders, Southern Methodist ";
Broncos_Roster[22] = " C.J. Anderson, California, RB ";
Broncos_Roster[13] = " Trevor Siemian, North Western, QB ";
Broncos_Roster[32] = " Andy Janovich, Nebraska, FB ";
Broncos_Roster[95] = " Derek Wolfe , Cincinnati, DE ";
Broncos_Roster[94] = " Domata Peko Sr. , Michigan State, NT ";
Broncos_Roster[93] = " Jared Crick, Nebraska, DE ";
Broncos_Roster[58] = " Von Miller, Texas A & M, WLB ";
Broncos_Roster[54] = " Brandon Marshall, Nevada, ILB ";
Broncos_Roster[51] = " Todd Davis, Sacramento State, ILB ";
Broncos_Roster[21] = " Aqib Talib, Kanas, LCB ";
Broncos_Roster[27] = " Brendan Langley, Lamar, RCB ";
Broncos_Roster[31] = " Justin Simmons , Boston College, SS ";
Broncos_Roster[26] = " Darian Stewart , South Carolina , FS ";
Broncos_Roster[8] = " Brandon McManus, Temple, PK ";
Broncos_Roster[9] = " Riley Dixon , Syracuse, P ";
Broncos_Roster[42] = " Casey Kreiter , Iowa, LS ";
Broncos_Roster[14] = " Cody Latimer , Indiana KR ";
Broncos_Roster[84] = " Isaiah Mckenzie, Georgia, PR ";
for (auto &it : Broncos_Roster)
cout << it.first << " => " << it.second << '\n';
vector<string> phoneticAlphabet;
cout << " This is the Phonetic Alphabet" ;
phoneticAlphabet.emplace_back("Alpha") ;
phoneticAlphabet.emplace_back("Bravo") ;
phoneticAlphabet.emplace_back("Charlie") ;
phoneticAlphabet.emplace_back("Delta") ;
phoneticAlphabet.emplace_back("Echo") ;
phoneticAlphabet.emplace_back("Foxtrot") ;
phoneticAlphabet.emplace_back("Golf") ;
phoneticAlphabet.emplace_back("Hotel") ;
phoneticAlphabet.emplace_back("India") ;
phoneticAlphabet.emplace_back("Juliett") ;
phoneticAlphabet.emplace_back("Kilo") ;
phoneticAlphabet.emplace_back("Lima") ;
phoneticAlphabet.emplace_back("Mike") ;
phoneticAlphabet.emplace_back("November") ;
phoneticAlphabet.emplace_back("Oscar") ;
phoneticAlphabet.emplace_back("Papa") ;
phoneticAlphabet.emplace_back("Quebec") ;
phoneticAlphabet.emplace_back("Romeo") ;
phoneticAlphabet.emplace_back("Sierra") ;
phoneticAlphabet.emplace_back("Tango") ;
phoneticAlphabet.emplace_back("Uniform") ;
phoneticAlphabet.emplace_back("Victor") ;
phoneticAlphabet.emplace_back("Whiskey") ;
phoneticAlphabet.emplace_back("Xray") ;
phoneticAlphabet.emplace_back("Yankee") ;
phoneticAlphabet.emplace_back("Zulu") ;
cout << " Vector size = " << phoneticAlphabet.size() << endl;
for (auto &itt : phoneticAlphabet)
cout << itt << "\n";
string query = " ";
cout << " Please select a letter to see the ! ";
//cin.ignore();
getline(cin, query);
string letters[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z"};
cout << "you entered a " << query ;
int index = 0;
for (int i = 0; i < 26; i++)
{
if (query == letters[i])
{ index = i; }
};
cout << " phonetic letter = " << phoneticAlphabet[index] << endl;
system("pause");
}
我感谢任何帮助!
答案 0 :(得分:2)
在您的情况下,没有必要将代码分成标题和源文件。
但是如果你想(例如,学习或实践),一般的想法是将函数声明放在.h文件和.cpp文件中的函数实现中。因此,如果您要创建一个函数populateRoster(),您可以将函数的单行声明放在头文件中:
void populateRoster();
然后是任何源文件中的完整函数文本:
void populateRoster()
{
// do something...
}
源文件必须#include头文件。然后你可以从main()或包含头文件的任何其他函数调用populateRoster()。
当然,即使没有创建头文件,你仍然可以从main()调用它,如果你只是在main()上面的main()中定义它。
答案 1 :(得分:1)
您无需分开代码。
但是,如果你有义务,那么:
使用函数初始化地图,使用初始化矢量的函数。将这两个函数放在源文件中,并为它提供头文件。
在main()
中包含该头文件,并用两次调用这些函数替换初始化代码,然后就完成了。
如果我有选择,我不会将文件中的代码分开,而是使用上述功能。
答案 2 :(得分:0)
由于您不想要class
,如果Broncos_Roster
和phoneticAlphabet
可以公开,只需将它们放在一个标题文件中 - 比如myHeader.h
;或者,将其内容放入cpp文件myHeader.cpp
,myHeader.h
只保留接口。
当然,您可以使用OOP设计概念,如:
namespace MyData { ... }
使用方法/函数获取地图和矢量,在myHeader.h
中如下:
const map<int, string>& get_Broncos_Roster() { // return Broncos_Roster; }
如果代码中的地图和矢量是常量,那么最好使用C ++新功能使它们保持不变,例如:
const map<int, string> Broncos_Roster = {
{ 88, " Demaryius Thomas, Georgia Tech, WR " },
{ 72, " Garret Bolles , Utah, LT " },
... ...
{ 14, " Cody Latimer , Indiana KR " },
{ 84, " Isaiah Mckenzie, Georgia, PR " },
};
// if it's static and const
static const vector<string> phoneticAlphabet = {
"Alpha",
"Bravo",
... ...
"Yankee",
"Zulu",
};
顺便说一下,将map和vector替换为常量:
msr -p your.cpp -t "Broncos_Roster\s*\[([^\]]+)\]\s*=\s*(.+?)\s*;" -o "{ $1, $2 }," -R
msr -p your.cpp -t "phoneticAlphabet.emplace_back\((.+)\);" -o "$1," -R
请参阅下面的屏幕截图:(msr.exe
/ msr.gcc*
是我的开放项目中的单个exe工具:tools
目录中的https://github.com/qualiu/msr。)
答案 3 :(得分:0)
考虑到你在主文件中写了所有内容,没有必要在源文件和头文件中分隔你的代码。你的头文件是空的,因为没有原型的函数,你的主要IS在源文件中。
如果由于某种原因你必须首先将你的main分成单独的函数,并将它们放在另一个源文件中,并带有它自己的头文件。