对于具体的名称结构的出口数据 [英] Structures export data for specific name
问题描述
这是我创建的结构:
的#include<&iostream的GT;
#包括LT&;串GT;
#包括LT&;&的fstream GT;
使用命名空间std;
学生结构
{
FIRST_NAME的char [10];
焦炭姓氏[10];
CHAR国家[20];
};
无效的主要()
{
学生阵列;
诠释N,I;
CIN>> N; 对于(i = 0; I< N;我++)
{
COUT<< 名称:;
CIN>> array.first_name;
COUT<< 姓:;
CIN>> array.last_name;
COUT<< 国家:;
CIN>> array.country; } 对于(i = 0; I< N;我++)
{
COUT<< array.first_name<< ;
COUT<< array.last_name<< ;
COUT<< array.country<< ;
}
系统(暂停);
}
我不能做的是...例如,我(在此行)输入名字John
COUT<< 名称:;
CIN>> array.first_name;
和我有写code,一旦我进入约翰(例如)
显示关于他的所有信息:姓的国家。而当我进入全国出口:名字,姓氏。也许我没有正确的解释。因为我的英语不好。也许这是我无法找到特定的信息或类似的例子的原因。
输出继电器例如:
姓名:John
姓:李四
国家:英格兰这是我不能做的部分:/信息有关学生/
输入名称进行检查:
约翰这里的输出必须是:
姓:李四
国家:英格兰
您需要在您存储所有学生的容器:我推荐使用的std ::矢量
。
的#include<矢量>的std ::矢量<学生和GT;学生们;
读取数据到一个局部变量,其附加到你的容器。
为(i = 0; I< N;我++)
{
学生的学生;
COUT<< 名称:;
CIN>> student.first_name;
COUT<< 名字:;
CIN>> student.last_name;
COUT<< 国家:;
CIN>> student.country; students.push_back(学生); //< - 附加学生的学生阵
}
通过你的容器迭代打印所有学生
/ *
1. students.begin();是,开始于在所述第一值的函数
要通过数据的数组
2. students.end();标志着结束
3.类型** **自动使用,自动获取到类型为您的变量,
这是更有效,因为不会有任何转换,你不必
担心类型的拼写错误
* /为(自动它= students.begin(!);它= students.end();它++)
//为(的std ::矢量<学生和GT;:迭代它= students.begin();它= students.end();它++!)//自动< - >的std ::矢量<学生和GT; ::迭代
{
COUT<< IT-> FIRST_NAME<< ;
COUT<< IT->姓氏<< ;
COUT<< IT->国家<< ;
}
这code是与上面类似:
的(为size_t我= 0; I< students.size();我++)
{
COUT<<学生[I] .first_name<< ;
COUT<<学生[I] .last_name<< ;
COUT<<学生[I] .country<< ;
}
如果你想找到它的名字一个学生,你必须使用 STRCMP
来比较名称。
为(自动它= students.begin(!);它= students.end();它++)
{
如果(STRCMP(IT-> FIRST_NAME,searchname)== 0)
{
...
}
}
That is the structure that I created:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Students
{
char first_name[10];
char last_name[10];
char country[20];
};
void main()
{
Students array;
int n, i;
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Name:";
cin >> array.first_name;
cout << "Last Name:";
cin >> array.last_name;
cout << "Country:";
cin >> array.country;
}
for (i = 0; i < n; i++)
{
cout << array.first_name << " ";
cout << array.last_name << " ";
cout << array.country << " ";
}
system("pause");
}
What I can't do is ... For example, I enter the name John (in this line)
cout << "Name:";
cin >> array.first_name;
And I have to write code that once I enter John (for example) displays all information about him: last name, country. And when I enter country to export: first name, last name. Maybe I don't explain it properly. Because my english is bad. Maybe that's the reason that i can't find specific information or similar examples.
Ouput example:
Name:John
Last Name: Doe
Country: England
And that's the part that i can't do:
/Info about student/
Enter Name for check:
John
and here the output must be:
Last Name: Doe
Country: England
You need a container where you store all your students: I recommend to use std::vector
.
#include <vector>
std::vector<Students> students;
Read your data into a local variable and append it to your container.
for (i = 0; i < n; i++)
{
Students student;
cout << "Name:";
cin >> student.first_name;
cout << "First name:";
cin >> student.last_name;
cout << "Country:";
cin >> student.country;
students.push_back( student ); // <- append student to array of students
}
Iterate through your container to print all students
/*
1. students.begin(); is a function that starts at the first value in
the array of data that you want to go through
2. students.end(); marks the end
3. the type **auto** is used, to automatically get the type for your variable,
it is more efficient since there will be no conversion and you don't have to
worry about type spelling errors
*/
for ( auto it = students.begin(); it != students.end(); it++ )
// for ( std::vector<Students>::iterator it = students.begin(); it != students.end(); it++ ) // auto <-> std::vector<Students>::iterator
{
cout << it->first_name << " ";
cout << it->last_name << " ";
cout << it->country << " ";
}
This code is similar to the above:
for ( size_t i = 0; i < students.size(); i++ )
{
cout << students[i].first_name << " ";
cout << students[i].last_name << " ";
cout << students[i].country << " ";
}
If you like to find a student by its name you have to use strcmp
to compare names.
for ( auto it = students.begin(); it != students.end(); it++ )
{
if ( strcmp( it->first_name, searchname ) == 0 )
{
...
}
}
这篇关于对于具体的名称结构的出口数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!