あなたは、プログラムの出力は、文字列が同じ長さを持っていることを想定している
3
ある
#include <iostream>
#include <string>
#include <numeric>
#include <functional>
int main()
{
std::string modelAnswer("ABABACDA");
std::string studentAnswer("ABADBDBB");
auto n = std::inner_product(modelAnswer.begin(), modelAnswer.end(),
studentAnswer.begin(), size_t(0),
std::plus<size_t>(), std::equal_to<char>());
std::cout << n << std::endl;
return 0;
}
例えばとしての標準的なアルゴリズムstd::inner_product
を使用することができます。それ以外の場合は、引数の最初のペアとしてless文字列を使用する必要があります。
例
#include <iostream>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
int main()
{
std::string modelAnswer("ABABACDA");
std::string studentAnswer("ABADBDBB");
auto n = std::inner_product(modelAnswer.begin(),
std::next(modelAnswer.begin(), std::min(modelAnswer.size(), studentAnswer.size())),
studentAnswer.begin(), size_t(0),
std::plus<size_t>(), std::equal_to<char>());
std::cout << n << std::endl;
return 0;
}
反復し、各要素を比較します。 – NathanOliver
2つの文字列charをcharで比較できます。 –
[std :: mismatch](http://en.cppreference.com/w/cpp/algorithm/mismatch)をループ内で使用して、終了イテレータを返すまで差異のセットを見つけることができます。 –