LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   stl programming (https://www.linuxquestions.org/questions/programming-9/stl-programming-627833/)

lppc2112 03-13-2008 02:14 PM

stl programming
 
Hello,

I have to use data structures an array or vector to store vertices in this program.

can anyone tell me how i can do it please?

#include<map>
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>

using std::istringstream;
using std::ifstream;
using std::string;
using std::cout;
using std::cin;
using std::cerr;
using std::map;
using std::multimap;


int main()
{
multimap<string,int> words;
map<int,string> lines;
string str;
ifstream input("test.txt"); //create file for input
if(input.fail())
{
cerr<<"\nThe file could not be opened.";
return -1;
}
int i=1;
while(getline(input,str))
{
istringstream in(str);
string s;
while(in>>s)
{
words.insert(make_pair(s,i));
}
lines.insert(make_pair(i,str));
i++;
}
string search;
cout<<"\nEnter a word to search: ";
cin>>search;
cout<<"\nThe number of matches = "<<words.count(search)<<'\n';
multimap<string,int>::iterator it1=words.lower_bound(search);
multimap<string,int>::iterator it2=words.upper_bound(search);
while(it1!=it2)
{
int x=it1->second;
map<int,string>::iterator iter=lines.find(x);
cout<<'\n'<<x<<" ) "<<iter->second<<'\n';
it1++;
while(true)
{
if(it1!=it2 && it1->second==x)
{
it1++;

}
else
{
break;
}
}
}

cout<<'\n';

return 0;
}


The final output result of the program should be a
modified obj-file.
I need the data structures (array or vector) only to
store the information, to access the data with the
algorithm and to store the modified data that will then be
written to a new obj-file

> 1. using array to store the vertices, face and normals
> 2. use array to count number of vertices, face and normals

binarybob0001 03-13-2008 09:27 PM

Well, I don't know if this answers your question, but here's my shot at it.
std::vector has a push_back function which will append data to the end of the vector. Accessing elements is easy through the [] operator, and getting a total count is easy through the size() function. I think you already know this stuff; that's why I'm a little confused. I recommend you break up main into smaller functions, such as, FileManager::openFile,
FileManager::getNextVertex(), etc where FileManager is a class used to get information. This way, if there is an error somewhere, you can find out where it came from more easily. Not to mention your code will be easier to follow not just for me but for you later on. Good luck.

matthewg42 03-13-2008 09:38 PM

Please post code in [code] tags which will preserve formatting and aid readability.

Also, this looks very much like a homework question. Please see the LQ rules for notes on that.

acid_kewpie 03-20-2008 10:02 AM

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.


All times are GMT -5. The time now is 11:11 AM.