I suggest that you put vector <contactDetails> proContactFile;
inside a class and that you add member functions for searching inside the vector to that class.
I'm assuming that you already have functions for reading the file and putting it in your vector <contactDetails> proContactFile;
in place, but since you didn't show them, I've added examples for that here too.
C++14 example:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
struct contactDetails {
std::string name;
std::string nickName;
std::string phoneNumber;
std::string carrier;
std::string address;
};
// read one contactDetails from a stream
std::istream& operator>>(std::istream& is, contactDetails& d) {
char sep = ','; // comma separated
std::getline(is, d.name, sep);
std::getline(is, d.nickName, sep);
std::getline(is, d.phoneNumber, sep);
std::getline(is, d.carrier, sep);
std::getline(is, d.address); // newline
return is;
}
// write one contactDetails to a stream
std::ostream& operator<<(std::ostream& os, const contactDetails& d) {
char sep = ','; // comma separated
os << d.name << sep << d.nickName << sep << d.phoneNumber << sep << d.carrier
<< sep << d.address << '\n';
return os;
}
// A class to keep the vector<contactDetails> proContactFile
// with some added support functions
struct Contacts {
// a member function to read the complete file and put it in the vector.
bool read_file() {
std::ifstream fin(filename);
if(fin) {
proContactFile.clear();
std::copy(std::istream_iterator<contactDetails>(fin),
std::istream_iterator<contactDetails>{},
std::back_inserter(proContactFile));
return true;
}
return false;
}
// a member function to write the complete vector out to a file
bool write_file() const {
std::ofstream fout(filename);
if(fout) {
std::copy(proContactFile.begin(), proContactFile.end(),
std::ostream_iterator<contactDetails>(fout));
return true;
}
return false;
}
// add proxy functions to the vector that might come in handy, like size()
auto size() const { return proContactFile.size(); }
// ... and iterator access to the vector
auto cbegin() const { return proContactFile.cbegin(); }
auto cend() const { return proContactFile.cend(); }
auto begin() const { return cbegin(); }
auto end() const { return cbegin(); }
auto begin() { return proContactFile.begin(); }
auto end() { return proContactFile.end(); }
// add a contactDetails to the vector
template< class... Args >
decltype(auto) emplace_back( Args&&... args ) {
return proContactFile.emplace_back(std::forward<Args>(args)...);
}
// erase one contactDetails using an iterator
template<typename It>
auto erase(It it) { return proContactFile.erase(it); }
// find functions
auto find_name(const std::string& name) {
return std::find_if(begin(), end(), [&name](const contactDetails& d) {
return name == d.name;
});
}
auto find_name(const std::string& name) const {
return std::find_if(cbegin(), cend(), [&name](const contactDetails& d) {
return name == d.name;
});
}
private:
const char* const filename = "Contact.csv";
std::vector<contactDetails> proContactFile;
};
Example usage:
int main() {
Contacts contracts;
contracts.read_file();
auto it = contracts.find_name("Yashodhara");
if(it != contracts.end()) {
std::cout << "FOUND: " << *it;
}
// you can then make changes to the vector and save it
// contracts.write_file();
}
Output:
FOUND: Yashodhara,Yash,711256677,Mobitel No. 29,Bollatha,Ganemulla