Hello, I am getting an error with all of the operators in my program... I would understand if I had not included <string> or <vector>, but aren't these operators defined in these files? Can someone help me figure out why these operators are not defined in my program?
I am using Visual C++ and the general error I am getting is:
error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for'const std::reverse_iterator<_RanIt> &' from 'unsigned int'
#include <vector>
#include <cstdio>
#include <iostream>
#include <string>
usingnamespace std;
// Function finds longest strictly increasing subsequence.
// O(n log k) algorithm.
void find_lis(vector<string> &a, vector<string> &b)
{
vector<string> p(a.size());
int u, v;
if (a.empty())
return;
b.push_back(0);
for (size_t i = 1; i < a.size(); i++)
{
// If next element a[i] is greater than last element of current longest subsequence a[b.back()], just push it at back of "b" and continue
if (a[b.back()] < a[i])
{
p[i] = b.back();
b.push_back(i);
continue;
}
// Binary search to find the smallest element referenced by b which is just bigger than a[i]
// Note : Binary search is performed on b (and not a). Size of b is always <=k and hence contributes O(log k) to complexity.
for (u = 0, v = b.size()-1; u < v;)
{
int c = (u + v) / 2;
if (a[b[c]] < a[i]) u=c+1;
else v=c;
}
// Update b if new value is smaller then previously referenced value
if (a[i] < a[b[u]])
{
if (u > 0) p[i] = b[u-1];
b[u] = i;
}
}
for (u = b.size(), v = b.back(); u--; v = p[v]) b[u] = v;
}
// test
int main()
{
string series;
cout << "Please enter a series of letters: " << endl;
cin >> series;
vector<string> seq(series, series+sizeof(series)/sizeof(series[0])); // seq : Input Vector
vector<string> lis; // lis : Vector containing indexes of longest subsequence
find_lis(seq, lis);
//Printing actual output
for (size_t i = 0; i < lis.size(); i++)
printf("%d ", seq[lis[i]]);
printf("\n");
system ("pause");
return 0;
}
You are causing a problem because you are trying to stick an int into a string. If you are trying to put a numeral in there, then cast it to a char if it is a single digit like: b.push_back(char(i+'0'));. If not, then use a stringstream object like:
1 2 3 4
int i;
stringstream ss(i);
vector<string> b;
b.push_back(ss.str())
Likewise things like:
1 2 3
vector<string> p;
p.push_back("Hello")l
int i = p[0];
That's the same problem but reversed.
I think stringstream is your friend here.
Another thing that I'd recommend is using iterators. Instead of doing this:
1 2 3 4
int i;
vector<string> b;
for (i = 0; i < b.size(); i++)
cout << b[i];
Try this:
1 2 3 4
vector<string>::iterator iter;
vector<string> b;
for (iter = b.begin(); iter < b.end(); iter++)
cout << *iter;