copying string from a vector to another

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 7
Reputation: arfmal is an unknown quantity at this point 
Solved Threads: 0
arfmal arfmal is offline Offline
Newbie Poster

copying string from a vector to another

 
0
  #1
Mar 6th, 2007
I have a problem - to copy a string from a vector and put it into another vector. It's ok if i use a string variable, where this variable will be used as a temp variable between these vectors....But i do not know what is the best approach to copy or "transfer" this string.

Can somebody give me some idea how to tackle this problem?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: copying string from a vector to another

 
0
  #2
Mar 6th, 2007
something like this example??
  1. vector<stirng> array1;
  2. vector<string> array2;
  3. array1.push_back("Hello World");
  4.  
  5. // copy the first string in array1 into array2
  6. array2.push_back(array1[0]);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 7
Reputation: arfmal is an unknown quantity at this point 
Solved Threads: 0
arfmal arfmal is offline Offline
Newbie Poster

Re: copying string from a vector to another

 
0
  #3
Mar 6th, 2007
Yes. Thanks. Another question if you don't mind.

Once i "push back" this string into the 2nd vector, then how will i compare the content of both vector?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: copying string from a vector to another

 
0
  #4
Mar 7th, 2007
Not clear why would you want to compare 2 vectors (it's a sequential container) but...
First compare the sizes of them if that matches Write a for loop and compare element by element.. !
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 7
Reputation: arfmal is an unknown quantity at this point 
Solved Threads: 0
arfmal arfmal is offline Offline
Newbie Poster

Re: copying string from a vector to another

 
0
  #5
Mar 7th, 2007
Originally Posted by thekashyap View Post
Not clear why would you want to compare 2 vectors (it's a sequential container) but...
First compare the sizes of them if that matches Write a for loop and compare element by element.. !

Actually i'm working on a set of data where i need to count every string i have which is located in the vector. In order to count each data i need to compare all of them and start counting for the similarity of data. For example;

In my vector, the data

{"0023", "0002", "0045", "5568"}
{"0023", "0002", "0030", "5550"}

What i need to do: firstly chose the 1st data from the first string, put it into the next vector. Then from there, i need search this data in the second string in the previous vector. If i found the same data, count it.

You can check my code if you want to. Something wrong..

string TempExam;
while(i <TempStudentVec.size())
{
ExamIter = StudIter[i].Exam.begin();
TEMPLarge.NoOfStudent=0;

if(StudIter[i].Exam.size())
{
TempExam = ExamIter[0];
TEMPLarge.Exam.push_back(TempExam);
TEMPLarge.NoOfStudent++;
StudIter[i].Exam.erase(StudIter[i].Exam.begin());
j=1;
while(j < TempStudentVec.size())
{
ExamIter2 = StudIter[j].Exam.begin();
k =0;
while (k<StudIter[j].Exam.size())
{
if (ExamIter2[0] == TEMPLarge.Exam)
{
TEMPLarge.NoOfStudent++;
............................
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: copying string from a vector to another

 
0
  #6
Mar 7th, 2007
  1. string TempExam;
  2. while(i <TempStudentVec.size())
  3. {
  4. ExamIter = StudIter[i].Exam.begin();
  5. TEMPLarge.NoOfStudent=0;
  6.  
  7. if(StudIter[i].Exam.size())
  8. {
  9. TempExam = ExamIter[0];
  10. TEMPLarge.Exam.push_back(TempExam);
  11. TEMPLarge.NoOfStudent++;
  12. StudIter[i].Exam.erase(StudIter[i].Exam.begin());
  13. j=1;
  14. while(j < TempStudentVec.size())
  15. {
  16. ExamIter2 = StudIter[j].Exam.begin();
  17. k =0;
  18. while (k<StudIter[j].Exam.size())
  19. {
  20. if (ExamIter2[0] == TEMPLarge.Exam)
  21. {
  22. TEMPLarge.NoOfStudent++;
  23. ............................
  24. }
  25. }
  26. }
  27. }
  28. }
It's seriously unreadable without code-tags.
Also after formatting I'm unable to understand what exactly is going on (what are you trying to do). Also the code is not complete.
May be with that info I can comment further.
Just by guessing I can say the following:
If you have 2 vectors v1 and v2. And you are trying to count the number of times every element of v1 is repeated in v2 easier way would be do use count()

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <functional>
  6.  
  7. using namespace std ;
  8.  
  9. int main()
  10. {
  11. string init_arr1[5] = { "ONE", "TWO", "THREE", "FOUR", "FIVE" };
  12. string init_arr2[10] = { "ONE", "FIVE", "FOUR", "FIVE", "THREE", "FOUR", "THREE", "FOUR", "ONE", "THREE" };
  13.  
  14. vector<string> v1(init_arr1, init_arr1 + 5 ) ;
  15. vector<string> v2(init_arr2, init_arr2 + 10 ) ;
  16. //vector<string> v1(init_arr1, init_arr1 + sizeof(init_arr1)/sizeof(*init_arr1)) ;
  17. //vector<string> v2(init_arr2, init_arr2 + sizeof(init_arr2)/sizeof(*init_arr2)) ;
  18.  
  19.  
  20. cout << "count() example" << endl ;
  21. //want to find how many times each string in v1 occures in v2
  22. vector<string>::iterator it = v1.begin() ;
  23. for( ; it != v1.end(); it++ )
  24. {
  25. size_t tmp_count = count( v2.begin(), v2.end(), *it ) ;
  26. cout << (*it).c_str() << " occured in v2 " << tmp_count << " time(s)." << endl ;
  27. }
  28.  
  29. int init_arri1[5] = { 1, 2, 3, 4, 5 };
  30. int init_arri2[10] = { 1, 5, 4, 5, 3, 4, 3, 4, 1, 3 };
  31.  
  32. vector<int> vi1(init_arri1, init_arri1 + 5 ) ;
  33. vector<int> vi2(init_arri2, init_arri2 + 10 ) ;
  34. //vector<int> vi1(init_arri1, init_arri1+sizeof(init_arri1)/sizeof(*init_arri1)) ;
  35. //vector<int> vi2(init_arri2, init_arri2+sizeof(init_arri2)/sizeof(*init_arri2)) ;
  36.  
  37. cout << endl << endl << "count_if() example" << endl ;
  38. //Here is an example of conditional find.. it counts the
  39. //numbers greater than given number (instead of equal)
  40. vector<int>::iterator iit = vi1.begin() ;
  41. for( ; iit != vi1.end(); iit++ )
  42. {
  43. size_t tmp_count = count_if( vi2.begin(), vi2.end(), bind2nd(greater<int>(), *iit) ) ;
  44. cout << tmp_count << " element(s) in vi2 are greater than " << (*iit) << endl ;
  45. }
  46.  
  47.  
  48. return 0 ;
  49. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC