我的源代码是否显示了我自己对c++中结构的理解
Does my source code shows my own understanding of structures in c++?
我想知道我是否真的走在了正确的方向上,我目前正在学习C++语言,并阅读Alex Allain的《跳进C++》一书,本章末尾有一个关于结构的实践问题,要创建一个联系人簿程序,用户不仅应该能够填写一个结构,而且应该能够添加新的条目,每个都有一个单独的名字和电话号码。让用户添加他或她想要的任意多的条目——这很容易做到吗?添加显示全部或部分条目的功能,允许用户浏览条目列表。
到目前为止,以下是我所做的,我想知道我的源代码是否真的是正确的,它是否表明了我对结构和整体c++的理解?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
struct user{
string name;
int phone_num;
};
int _tmain(int argc, _TCHAR* argv[])
{
int input, number; // will hold the users input at the beginning of the program
int counter = 0; // keep track of the array position
int const arraySize = 10; // size of the array
user new_username[arraySize]; // will hold the users details
string name; // will hold the users input for the name
cout << "CONTACTSn";
do{
cout << "+ADD [1] -EXIT[0]";
cin >> input;
if(input == 1){
//cout << counter;
cout << "nName: ";
cin >> name;
new_username[counter].name += name;
cout << endl << "nPhone: ";
cin >> number;
new_username[counter].phone_num = number;
counter++;
//set_user(counter);
}
cout << "Name Numbern";
cout << "--------------n";
for(int j=0; j < arraySize; j++){
cout << new_username[j].name;
cout << " -- ";
cout << new_username[j].phone_num;
cout << "n";
}
cout << "n";
}while(input != 0);
cout << "n";
system("PAUSE");
return 0;
}
Stackoverflow并不用于代码审查,但有一个不同的网站(尽管仍处于测试版(:https://codereview.stackexchange.com/
只是我注意到了一些快速的事情:
- 您的程序忽略无效输入(输入2、3或任何其他数字,而不是1或0(
- 您不检查
user
阵列是否已满 - 这并不是真正面向对象的
至于基本理解。。。我想是的,但这其实并不难。
要实现"允许用户添加任意数量的条目",您必须使用动态数组(询问用户要添加多少条目(或使用一些动态存储(例如链表(。
如果您希望用户能够添加他/她想要的任意多的联系人,您可以使用强大的标准模板机制。
对于这个应用程序,我建议查看
std::vector
或
std::map
这就是你使用它们的方式:(记住这是伪代码,不会编译(
#include <vector>
typedef struct {
std::string name;
double phoneNumber;
} YourStruct;
int main( int argc, char **argv ) {
std::vector<YourStruct> structVector;
do {
int input;
std::cin >> input;
if (input) {
//(read user input for name and number)
YourStruct yourStruct;
yourStruct.name = (user input)
yourStruct.phoneNumber = (user input)
// insert into the vector
structVector.push_back(yourStruct)
}
} while (input != 0)
// now to print what you have:
for (int i = 0; i < structVector.size(); i++) {
std::cout << structVector[i].name << ", " << structVector[i].number << std::endl;
}
}
使用矢量的好处是,它可以自动调整大小并跟踪其大小,而无需使用计数器项。
现在,为了一些更棘手的事情。我们将使用映射来使用"key"值来获取名称。以下代码不会编译,但它在功能上是如何执行任务的:
#include <map>
int main(int argc, char** argv) {
std::map<std::string,double> myMap;
// the string is the "key" value, which can be the name of the person
// while the "independent" is the phone number
do {
// determine if the user wants to put another entry in the map
if (insert) {
std::string name = (user input name)
double number = (user input number)
myMap[name] = number;
}
} while (input != 0)
// now we can iterate through the map
std::map<std::string,double>::iterator it;
for (it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << it.first << ", " << it.second << std::endl;
}
// also, you can look up by name
it = myMap.find("Tony Stark");
if (it != myMap.end()) { // if this condition is met, it means you found one
std::cout << it.first << ", " << it.second << std::endl;
}
}
总体而言,您的代码看起来不错。然而,它不是C++。你的编程就像一个C程序员。C++的美妙之处(当然,除了多面手之外(在于强大的模板库。
我只是给你一个小小的尝试,你可以做什么样的模板。如果您有任何问题,请发表评论。我们都经历过你现在的处境,因为你从书本中自学成才而受到赞扬。
从您的问题和代码来看,您似乎是一名新程序员,因此我将向您解释答案,并对您的代码进行一些注释。
为了解决"项目一样多"的问题,几乎没有什么方法。最简单的,可能也是一个很好的解决方案是使用map,在任何语言中它都有不同的名称。但通常名称是字典、关联数组。。。
使用地图将帮助您处理:
- 尽可能多的项目
- 按名称排序的订单
- 过滤会更容易,这取决于你想做什么,以及你的过滤器有多复杂
我谈到的其他方法要基本得多,包含的代码也要多得多,但它们给你的感觉是如何自己实现map对象,但这是一个不同的问题。
在我上面提到的链接中,示例是用于电话输入的。但是,如果您仍然想使用struct,可以将键作为名称,将值作为struct本身。这样做的一个理由是,以后你计划添加地址和公司名称。
关于代码的一些注意事项。
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
//use meanigful name, instead of user it can be phoneEntry
struct user{
string name;
//Starting using the following conventions using the capital letter in variable names for example phoneNumber
//int不会是电话号码的神,因为有时你需要星号或hash,或者一个前导零,也许是一个加号。它最好使用字符串来表示tat。当然,每次收到用户输入时,都应该对其进行验证int phone_num;};
//Why the name of the function is not main, why its _tmain
int _tmain(int argc, _TCHAR* argv[])
{
//Keep going with your comments, with time you would imbrase your own style based on things that you will see. But in general commenting is very important
//Give meanigful name, instead input userCommand for example
int input, number; // will hold the users input at the beginning of the program
int counter = 0; // keep track of the array position
int const arraySize = 10; // size of the array
//Again meangful names
user new_username[arraySize]; // will hold the users details
string name; // will hold the users input for the name
cout << "CONTACTSn";
do{
cout << "+ADD [1] -EXIT[0]";
cin >> input;
if(input == 1){
//cout << counter;
cout << "nName: ";
cin >> name;
new_username[counter].name += name;
cout << endl << "nPhone: ";
cin >> number;
new_username[counter].phone_num = number;
counter++;
//set_user(counter);
}
cout << "Name Numbern";
cout << "--------------n";
for(int j=0; j < arraySize; j++){
cout << new_username[j].name;
cout << " -- ";
cout << new_username[j].phone_num;
cout << "n";
}
cout << "n";
}while(input != 0);
cout << "n";
system("PAUSE");
return 0;
}
我希望它能帮助
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- C++概念:如何使用'concept'检查模板化结构的属性?