944,204 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3407
  • C++ RSS
Mar 6th, 2007
0

copying string from a vector to another

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arfmal is offline Offline
7 posts
since Feb 2007
Mar 6th, 2007
0

Re: copying string from a vector to another

something like this example??
C++ Syntax (Toggle Plain Text)
  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]);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Mar 6th, 2007
0

Re: copying string from a vector to another

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arfmal is offline Offline
7 posts
since Feb 2007
Mar 7th, 2007
0

Re: copying string from a vector to another

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.. !
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 7th, 2007
0

Re: copying string from a vector to another

Click to Expand / Collapse  Quote originally posted by thekashyap ...
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++;
............................
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arfmal is offline Offline
7 posts
since Feb 2007
Mar 7th, 2007
0

Re: copying string from a vector to another

C++ Syntax (Toggle Plain Text)
  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()

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Won't read .h
Next Thread in C++ Forum Timeline: need help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC