如何列出向量中存储的结构迭代次数
How to list how many iterations of a struct are stored inside a vector?
我是 c++ 的新手(以及一般的编码(,最近一直在使用向量中保存的结构,在这种情况下:
struct Contact{
string name;
string address;
string phone;
string email;};
vector<Contact> contacts;
因此,我的一个函数涉及搜索每个联系人,以查找名称中存储的字符串与搜索输入匹配的联系人。为此,我做了一个 for 循环:
for(int i = 0; i < contacts.size(); i++){
if(contacts[i].name == searchInput){
cout << contacts[i].address << "nr" << contacts[i].phone << "nr" << contacts[i].email;
但是由于某种原因,这只能找到正确的联系人,如果它是存储在以下位置的名称:
contacts[0].name
没有其他人。因此,在试图找出问题所在时,我决定这样做
cout << contacts.size();
我认为应该输出 3,因为我只存储了三个联系人。然而,由于某种原因,它输出 7。无论如何,我是否可以准确列出存储在联系人向量中的联系人迭代次数以使我的 for 循环正常工作?
编辑我的完整代码:
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
struct Contact
{
string name;
string address;
string phone;
string email;
};
bool go;
bool a = false;
char command;
string endL = "nr";
string tab = "t";
string line;
int i;
int counter = 0;
int contactCounter = 0;
vector<Contact> contacts;
void add(){
contacts.push_back(Contact());
int newcontact = contacts.size() - 1;
string input;
cout << "Enter the name: " << endL;
cin >> input;
contacts[newcontact].name = input;
cout << "Enter the address: " << endL;
cin >> input;
contacts[newcontact].address = input;
cout << "Enter the phone number: " << endL;
cin >> input;
contacts[newcontact].phone = input;
cout << "Enter the email address: " << endL;
cin >> input;
contacts[newcontact].email = input;
}
void search(string name){
for(int i = 0; i < contacts.size(); i++){
if(contacts[i].name == name){
cout << "Name: " << contacts[i].name << endL << "Address: " << contacts[i].address << endL << "Phone Number: " << contacts[i].phone << endL << "Email: " << contacts[i].email << endL << endL;
a = true;
}
}
if(a == false){
cout << "There is no contact under that name." << endL;
}
}
int main() {
ifstream phonebook;
phonebook.open("phonebook.txt");
if(phonebook.is_open()){
while(getline(phonebook,line)){
if(line.empty() == false){
if(counter % 4 == 0){
contacts.push_back(Contact());
contacts[contactCounter].name = line;
}else if(counter % 4 == 1){
contacts[contactCounter].address = line;
}else if(counter % 4 == 2){
contacts[contactCounter].phone = line;
}else if(counter % 4 == 3){
contacts[contactCounter].email = line;
contactCounter++;
}
counter++;
}
}
}else{cout << "an error has occurred while opening the phonebook";}
phonebook.close();
cout << contacts.size() << endL;
cout << "Enter a command." << endL << tab << "To add a contact, enter '+'" << endL << tab << "To search for a contact, enter 's'" << endL << tab << "To delete a contact, enter '-'" << endL << tab << "To quit the program, enter 'q'" << endL;
cin >> command;
while(command != 'q'){
if(command == '+'){
add();
command = '/';
}
else if(command == 's'){
string searched;
cout << "Please enter who you would like to search for: ";
cin >> searched;
search(searched);
command = '/';
}
else if(command == '-'){
cout << "Not done." << endL;
command = '/';
}
else if(command == '/'){
cout << "Enter a command." << endL << tab << "To add a contact, enter '+'" << endL << tab << "To search for a contact, enter 's'" << endL << tab << "To delete a contact, enter '-'" << endL << tab << "To quit the program, enter 'q'" << endL;
cin >> command;
}
else{
cout << "That command is invalid." << endL;
cout << "Enter a command." << endL << tab << "To add a contact, enter '+'" << endL << tab << "To search for a contact, enter 's'" << endL << tab << "To delete a contact, enter '-'" << endL << tab << "To quit the program, enter 'q'" << endL;
cin >> command;
}
}
ofstream newbook;
newbook.open("phonebook.txt");
if(newbook.is_open()){
for(int i=0; i < contacts.size(); i++){
newbook << contacts[i].name << endl;
newbook << contacts[i].address << endl;
newbook << contacts[i].phone << endl;
newbook << contacts[i].email << endl;
newbook << endL;
}
}else{cout << "there was an issue saving your contacts" << endL;}
newbook.close();
return 0;
}
除了这一行之外,你的代码实际上没有任何问题
string endL = "nr";
这真的应该只是
string endL = "n";
n
会自动转换为系统使用的行尾,传统上在Unix系统上n (0x0a)
,在Windows上rn (0x0d0a)
。
但是,这对该计划有何影响?好吧,它只有在程序末尾写入电话簿后才会生效,以便phonebook.txt
包含这些在末尾(在Windows上(rnr
的虚假行尾。因此,当读取文件时,它会读取直到新行rn
,并将rPerson Name
视为后面的行!这就解释了为什么搜索失败了。
您还可能会看到生成的一些其他虚假联系人,因为末尾可能有一些额外的r
,每个读作一行。如果不看你的phonebook.txt
我不能确定为什么你有额外的 4,尽管我猜额外的 r
s 会是原因。
总而言之,将n
用于新行。
要回答标题,vector::size()
是获取向量中存储对象数量的方法。这不是在骗你。
使用基于范围的 for 循环可确保您不会击中任何不存在的联系人:
for(auto&& contact: contacts)
{
// Contact contact is now accessible.
}
此外,将a
存储为全局变量可能不是一个好主意。如果执行两次search
会怎样?
- C++结构编译器错误询问向量的迭代器
- 如何使用迭代器指向结构体c++的向量
- 在VS 2017中,朋友通过具有私有析构函数的结构的unique_ptr向量进行迭代失败
- 迭代结构中包含的向量
- 为结构的向量创建C 矢量迭代器
- 是否有一个很好的方法可以在C 11中打印出像JSON一样的Trie结构(仅迭代解决方案)的扁平命名空间
- 有没有办法迭代不完整的结构
- 用于隐藏结构细节的迭代器适配器
- 在 stl 中迭代结构类型的队列
- 无法迭代结构"string"的哈希映射:[列表] C++
- Boost Intervocess:通过迭代通过从结构引用对象的映射进行迭代时
- 如何从数据结构的向量中收集迭代器
- 当迭代器添加到添加到列表向量的结构时,迭代器停止指向值?
- 使用POCO迭代JSON结构
- 是否值得两次通过文件进行迭代,以创建最小的数据结构
- 创建自定义迭代器结构以与CSTDIO一起使用
- 在编译时使用模板在跨结构字段中迭代
- 正在取消引用自定义结构字段的迭代器
- 使用C预处理器在结构字段上进行迭代
- 如何列出向量中存储的结构迭代次数