F
Fei Liu
What's wrong with the following code? It seems I can't assign to a
vector<string> element...
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <string>
using namespace std;
struct c{
vector<string> a;
void set_size(int n) { a.reserve(n); }
void set_val(int i, string v) { a = v; }
};
void foo(c & cc){
cc.set_size(10);
cc.set_val(3, "value 3");
}
int main(){
vector<int> a;
a.reserve(10);
a[3] = 10;
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
cout << "a[3]: " << a[3] << endl;
{
vector<string> a;
a.reserve(10);
a[3] = "value 3";
copy(a.begin(), a.end(), ostream_iterator<string>(cout, " "));
cout << "a[3]: " << a[3] << endl;
}
c cc;
foo(cc);
}
vector<string> element...
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <string>
using namespace std;
struct c{
vector<string> a;
void set_size(int n) { a.reserve(n); }
void set_val(int i, string v) { a = v; }
};
void foo(c & cc){
cc.set_size(10);
cc.set_val(3, "value 3");
}
int main(){
vector<int> a;
a.reserve(10);
a[3] = 10;
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
cout << "a[3]: " << a[3] << endl;
{
vector<string> a;
a.reserve(10);
a[3] = "value 3";
copy(a.begin(), a.end(), ostream_iterator<string>(cout, " "));
cout << "a[3]: " << a[3] << endl;
}
c cc;
foo(cc);
}