Using the Star structure defined in file p1 cpp write the fu
Solution
PROGRAM CODE:
//Modify the program ro suit your result.
#include <iostream>
#include <vector>
using namespace std;
//assuming this to be the structure for stars
struct Stars
{
string name;
int draperNum;
};
//function to find the star names in the vector
vector<int> findStars(vector<Stars> vStars, string part_star_name)
{
vector<int> numbers;
//iterating through the vector
vector<Stars>::iterator itr = vStars.begin();
while(itr != vStars.end())
{
Stars star = *(itr);
string starname = star.name;
//using find to look for the part of the string
int found = starname.find(part_star_name);
if(found != std::string::npos)
{
numbers.push_back(star.draperNum); // if found, then the corresponding draper number is pushed into the return vector
}
itr++;
}
return numbers;
}
int main()
{
Stars star = {\"Sirius\", 134};
Stars star2 = {\"vega\", 883};
vector<Stars> vStars;
vStars.push_back(star);
vStars.push_back(star2);
vector<int> numbers = findStars(vStars, \"veg\");
//iterating to see the result
vector<int>::iterator itr = numbers.begin();
while(itr != numbers.end())
{
cout<<*itr;
itr++;
}
return 0;
}
OUTPUT:
883

