I'm new to C++ programming. I want to make a code that shows elements which two char vectors
have and do not have in common.
I started by creating two string
by cin
and then pass them to vectors. But now I don't know how to compare the char
elements of both vector
s, or even how to get to those elements. I tried treating them as if they were int
and using set_difference
, but none of that worked.
I would love to hear any recommendations on how to complete my code.
Here is the part of my code that actually can run:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
string s1, s2;
vector<int> diff;
cin >> s1;
cin >> s2;
vector<char> v1(s1.begin(), s1.end());
vector<char> v2(s2.begin(), s2.end());
return 0;
}
First, you don't have to create vectors - you can work on strings as well. Also, the vector containing different characters should contain char
s, not int
s, so:
int main(){
string s1, s2;
vector<char> diff;
vector<char> same;
cin>>s1;
cin>>s2;
...
Now, if you want to check if the characters in s1
are different or same to s2
, you have to compare each element in s1
with each element in s2
.
for(const auto& it : s1) { // for each element in s1
bool different = true;
for(const auto& jt : s2) { // for each element in s2
if(it == jt) {
different = false; // if the elements are same, note it
break; // and break out of the loop
}
}
if(different) { // if this character wasn't same as any in s2, add it to the diff vector
diff.push_back(it);
} else {
same.push_back(it);
}
}
At this point diff
contains all elements of s1
that s2
doesn't contain and same
contains common elements. You need to append elements of s2
that s1
doesn't contain, too.
for(const auto& it : s2) {
bool different = true;
for(const auto& jt : s1) {
if(it == jt) {
different = false;
break;
}
}
if(different) {
diff.push_back(it);
}
}
This code snippet is very similar to the previous one - the only differences are that s2
is now the outer loop and s1
the inner one and elements that are same aren't added to the same
vector as they have already been added there.
You may not be familiar with the for(const auto& it: s1)
syntax. Here it is basically equivalent to for(size_t it; it < s1.size(); ++it)
(but they are pretty much different in general). For more detail google "range-based for loop".
As you can see comparing two unsorted arrays (in this case std::string
s, which are arrays of chars under the hood) is very inefficient. A much better container for this situation would be std::set
. It has its downsides as well, for example the elements must not repeat (if you try to insert an already existing element, nothing hapens), yet it suits this case perfectly. Google std::set to use it consciously.
Final code:
#include <set>
#include <iterator> // to use std::make_move_iterator
#include <string>
#include <iostream>
using namespace std;
int main() {
string temp1, temp2; // create temporary strings, because you can't insert data directly to set with cin
cin >> temp1;
cin >> temp2;
// move data from temporary strings to sets
set<char> s1(make_move_iterator(temp1.begin()), make_move_iterator(temp1.end()));
set<char> s2(make_move_iterator(temp2.begin()), make_move_iterator(temp2.end()));
set<char> diff; // it also can be a set
for(const auto& it : s1) {
if(s2.count(it) == 0) { // if the element of s1 is not present in s2, insert it to diff
diff.insert(it);
} else { // if it is present, insert it to same
same.insert(it);
}
}
for(const auto& it : s2) {
if(s1.count(it) == 0) {
diff.insert(it);
}
}
for(const auto& it : diff) {
cout << it;
}
cout << "\n"
for(const auto& it : same) {
cout << it;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.