| | |
copying string from a vector to another
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
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?
Can somebody give me some idea how to tackle this problem?
something like this example??
C++ Syntax (Toggle Plain Text)
vector<stirng> array1; vector<string> array2; array1.push_back("Hello World"); // copy the first string in array1 into array2 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.
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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++;
............................
}
C++ Syntax (Toggle Plain Text)
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++; ............................ } } } } }
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)
#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 ; }
![]() |
Similar Threads
- Array required, but java.lang.String and java.util.Vector found (Java)
- array required, but java.lang.String found and java.util.Vector found? (Community Introductions)
- vector<string> - way to find longest string? (C++)
Other Threads in the C++ Forum
- Previous Thread: Won't read .h
- Next Thread: need help
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






