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?

Recommended Answers

All 5 Replies

something like this example??

vector<stirng> array1;
vector<string> array2;
array1.push_back("Hello World");

// copy the first string in array1 into array2
array2.push_back(array1[0]);

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?

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.. !

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.Exam.begin();
TEMPLarge.NoOfStudent=0;

if(StudIter.Exam.size())
{
TempExam = ExamIter[0];
TEMPLarge.Exam.push_back(TempExam);
TEMPLarge.NoOfStudent++;
StudIter.Exam.erase(StudIter.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++;
............................
}

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++;
                ............................
                }
            }
        }
    }
}

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()

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <functional> 

using namespace std ;

int main()
{
    string init_arr1[5] = { "ONE", "TWO", "THREE", "FOUR", "FIVE" };
    string init_arr2[10] = { "ONE", "FIVE", "FOUR", "FIVE", "THREE", "FOUR", "THREE", "FOUR", "ONE", "THREE" };

    vector<string> v1(init_arr1, init_arr1 + 5 ) ;
    vector<string> v2(init_arr2, init_arr2 + 10 ) ;
    //vector<string> v1(init_arr1, init_arr1 + sizeof(init_arr1)/sizeof(*init_arr1)) ;
    //vector<string> v2(init_arr2, init_arr2 + sizeof(init_arr2)/sizeof(*init_arr2)) ;


    cout << "count() example" << endl ;
    //want to find how many times each string in v1 occures in v2
    vector<string>::iterator it = v1.begin() ;
    for( ; it != v1.end(); it++ )
    {
        size_t tmp_count = count( v2.begin(), v2.end(), *it ) ;
        cout << (*it).c_str() << " occured in v2 " << tmp_count << " time(s)." << endl ;
    }

    int init_arri1[5] = { 1, 2, 3, 4, 5 };
    int init_arri2[10] = { 1, 5, 4, 5, 3, 4, 3, 4, 1, 3 };

    vector<int> vi1(init_arri1, init_arri1 + 5 ) ;
    vector<int> vi2(init_arri2, init_arri2 + 10 ) ;
    //vector<int> vi1(init_arri1, init_arri1+sizeof(init_arri1)/sizeof(*init_arri1)) ;
    //vector<int> vi2(init_arri2, init_arri2+sizeof(init_arri2)/sizeof(*init_arri2)) ;

    cout << endl << endl << "count_if() example" << endl ;
    //Here is an example of conditional find.. it counts the
    //numbers greater than given number (instead of equal)
    vector<int>::iterator iit = vi1.begin() ;
    for( ; iit != vi1.end(); iit++ )
    {
        size_t tmp_count = count_if( vi2.begin(), vi2.end(), bind2nd(greater<int>(), *iit) ) ;
        cout << tmp_count << " element(s) in vi2 are greater than " << (*iit) << endl ;
    }


    return 0 ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.