zadania z thinking in c++

0

Mam problem z zadaniem z końca 2 rozdziału.

  1. "Zmień program Fillvector.cpp w taki sposób, by drukował wiersze od końca - od ostatniego do pierwszego."

Próbowałem czegoś takiego, ale są błędy.

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {
  vector<string> v;
  ifstream in("Fillvector.cpp");
  string line;
  while(getline(in, line))
    v.push_back(line); // Dodanie wiersza na koncu
  // Dodanie numerow wierszy:
 for(int i = v.end(); i != v.begin(); i--)
   cout << i << ": " << v[i] << endl;
} ///:~

Błędy:

 
In function 'int main()':|20|error: cannot convert '__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >' to 'int' in initialization|

|20|error: no match for 'operator!=' in 'i != v.std::vector<_Tp, _Alloc>::begin [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]()'|
||=== Build finished: 2 errors, 0 warnings ===|
0

Nie umiesz czytać błędów i wyciągać wniosków/szukać w google? Przecież tam w pętli zamiast iteratora masz zwykłą zmienną i, a co wg Ciebie zwraca v.end()? ;> Iterator, no nie?

0

Czyli powinno być coś w stylu

  for(vector<string>::iterator i = v.end(); i != v.begin(); i--)
   cout << *i << ": " << v[i] << endl;

?

1

Jak już, to tak:

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {
  vector<string> v;
  ifstream in("Fillvector.cpp");
  string line;
  while(getline(in, line))
    v.push_back(line); // Dodanie wiersza na koncu
  // Dodanie numerow wierszy:
  int j=0;
 for(vector<string>::iterator i = v.end(); i != v.begin(); i--){
   cout << j << ": " << *i << endl;
   j++;
}
} ///:~
0

I jest problem (niedawno miałem podobny). Używam Code::Blocks. Program kompiluje się bez błędów. Niestety po uruchomieniu Windows raportuje błąd. W wierszu poleceń wyświetla się tylko 0:. Później jest błąd (AppName: fillvector.exe AppVer: 0.0.0.0 ModName: fillvector.exe ModVer: 0.0.0.0 Offset: 0006a7c3) i koniec programu.

Pobieram właśnie Visual C++ Express, być może problem leży po stronie IDE.

1

bo ten kod jest zły. Jak już chcesz od tyłu to przykładowo tak:

 for(vector<string>::reverse_iterator i = v.rbegin(); i != v.rend(); ++i){
   cout << j << ": " << *i << endl;
   j++;
}

ewentualnie:

for(vector<string>::iterator i = v.end() - 1; i != v.begin() - 1; --i){
   cout << j << ": " << *i << endl;
   j++;
}

lub

for(vector<string>::iterator i = v.end() - 1; i >= v.begin(); --i){
   cout << j << ": " << *i << endl;
   j++;
}

edit: na przyszłość proponowałbym zajrzeć tutaj

0
  int ciag = v.size();
  for(int i = 0; i < ciag; i++) {
    int lala = ciag-i-1;
    cout << lala << ": " << v[lala] << endl;
  }
0

A ja mam to tak i dziala!

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main() {
vector<string> v;
ifstream in("Lekcja.cpp");
string line;
while(getline(in, line))
v.push_back(line);
for(int i = v.size() - 1; i != -1; i--)
cout << i << ": " << v[i] << endl;
}

0

PS: zamiast jedynek kotki (chodzi mi o takie kratki #)
PS2 : ten post do usunięcia później.

jestem tylko gościem. ;(

0

A zadanie polegało na wstawieniu dwóch literek r i zapoznania się z reverse_iterator

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
 
int main() {
  vector<string> v;
  ifstream in("Fillvector.cpp");
  string line;
  while(getline(in, line))
    v.push_back(line); // Dodanie wiersza na koncu
  // Dodanie numerow wierszy:
 for(int i = v.rbegin(); i != v.rend(); ++i)
   cout << i << ": " << v[i] << endl;
}
0
_13th_Dragon napisał(a):

A zadanie polegało na wstawieniu dwóch literek r i zapoznania się z reverse_iterator

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
 
int main() {
  vector<string> v;
  ifstream in("Fillvector.cpp");
  string line;
  while(getline(in, line))
    v.push_back(line); // Dodanie wiersza na koncu
  // Dodanie numerow wierszy:
 for(int i = v.rbegin(); i != v.rend(); ++i)
   cout << i << ": " << v[i] << endl;
}

wcale nie. trzeba było użyć tylko tego co było w tej książce w tym rozdziale. koniec kropka kurza stopka

1 użytkowników online, w tym zalogowanych: 0, gości: 1