Параметри пошуку (Сторінка 4 з 6)
Ласкаво просимо!
Ласкаво просимо вас на україномовний форум з програмування, веб-дизайну, SEO та всього пов'язаного з інтернетом та комп'ютерами.
Будемо вдячні, якщо ви поділитись посиланням на Replace.org.ua на інших ресурсах.
Для того щоб створювати теми та надсилати повідомлення вам потрібно Зареєструватись.
Теми користувача Mirek7098 Виконати пошук
Знайдені повідомлення: з 61 по 80 з 101
Yola написав:Сортування машин за кольором:
int main()
{
class Car {
string color;
public:
Car(string color) : color(color) {}
string Color() const { return color; }
};
vector<Car> cars{ { "CC" },{ "AB" },{ "AA" },{ "CA" } };
sort(cars.begin(), cars.end(), [](const Car& l, const Car& r) { return l.Color() < r.Color(); });
for (auto& c : cars) cout << c.Color() << endl;
system("pause");
}
Компілятор чомусь матюкається на функцію sort
koala написав:Ну то уявіть собі, що it->Color - це звичайна функція. Чого не вистачає?
it->Color();
З звичайними функціями - так, а з методами класу - не дуже
То як мені його виправити? Я прибирав "*", тоді мені пише: error C3867: 'Car::Color': function call missing argument list; use '&Car::Color' to create a pointer to member
Компілятор пише: error C2276: '*' : illegal operation on bound member function expression
#include <iostream>
#include <conio.h>
#include <vector>
#include <iterator>
#include <string>
using namespace std;
class Car
{
private:
int year;
string color;
string brand;
string typefuel;
public:
Car()
{}
Car(int year,string color,string brand,string typefuel)
{
this->brand = brand;
this->color = color;
this->typefuel = typefuel;
this->year = year;
}
int Year()
{
return year;
}
string Color()
{
return color;
}
string Typefuel()
{
return typefuel;
}
string Brand()
{
return brand;
}
friend ostream& operator<< (ostream& stream, const Car& c);
bool operator==(const Car& c) const;
~Car(){}
};
ostream& operator<< (ostream& stream, const Car& c)
{
stream << "Color " << c.color << " | Year " << c.year << " | Type fuel " << c.typefuel << " | Brand " << c.brand << endl;
return stream;
}
bool Car::operator==(const Car & c) const
{
if (this->Color==c.Color)
return true;
}
void main()
{
int year;
string color;
string brand;
string typefuel;
const int n = 3;
vector<Car> list(n);
for(int i = 0; i < n; i++)
{
cout << "Enter color: ";
cin >> color;
cout << "Enter year: ";
cin >> year;
cout << "Enter brand: ";
cin >> brand;
cout << "Enter type fuel: ";
cin >> typefuel;
Car asd(year,color,brand,typefuel);
list.push_back(asd);
}
vector<Car>::iterator it;
for(it = list.begin(); it != list.end(); ++it)
{
cout << *it;
}
cout << "...:::Sort by color:::....\n";
for(it = list.begin(); it != list.end(); ++it)
{
cout << *it->Color;
for(vector<Car>::iterator at = list.begin; at != list.end();at++)
{
if(*it->Color == *at->Color)
{
сout << *at->Color;
}
}
}
_getch();
}
Я не розумію, чому воно не працює.Тут напевно проблема з перегрузкою, і з конструктором по замовчуванню.
Допоможіть будь-ласка виправити.
Це вказівник на нульовий елемент вектора
#include <iostream>
#include <conio.h>
#include <vector>
#include <iterator>
#include <string>
using namespace std;
class Car
{
private:
int year;
string color;
string brand;
string typefuel;
public:
Car(int year,string color,string brand,string typefuel)
{
this->brand = brand;
this->color = color;
this->typefuel = typefuel;
this->year = year;
}
friend ostream& operator<< (ostream& stream, const Car& c);
~Car(){}
};
ostream& operator<< (std::ostream& stream, const Car& c)
{
stream << "Color " << c.color << " | Yaer " << c.year << " | Type fuel " << c.typefuel << " | Brand " << c.brand << endl;
return stream;
}
void main()
{
int year;
string color;
string brand;
string typefuel;
const int n = 2;
vector<Car*> list(n);
for(int i = 0; i < n; i++)
{
cout << "Enter color: ";
cin >> color;
cout << "Enter year: ";
cin >> year;
cout << "Enter brand: ";
cin >> brand;
cout << "Enter type fuel: ";
cin >> typefuel;
Car *asd = new Car(year,color,brand,typefuel);
list.push_back(asd);
}
vector<Car*>::iterator it;
for(it = list.begin(); it != list.end(); ++it)
{
cout << *it;
}
_getch();
}
Чому після перегрузки оператора, мені виводить сміття?
А, я поняв, вже не треба (vector<Car*>::iterator it)
const int n = 5;
vector<Car*> list(n);
vector<Car>::iterator it;
.............................
...................................
.....якийсь код...................
for(it = list.begin(); it != list.end(); ++it)
{
}
У цьому випадку компілятор там де знак "=" і знак "!=" матюкається. Допоможіть виправити
Може не прокатить, бо є кольори що починаються на одинакові букви(Blue-Black,Green - Grey)
FakiNyan написав:ну то берете першу букву та й порівнюєте з першою буквою інших елементів
Може не прокатить, бо є кольори що починаються однаковими буквами(Blue-Black,Green-Grey)
FakiNyan написав:Mirek7098 написав:Колір заданий текстом
типу Black, White, Red ?
Так
То як відсортувати машини за кольором? Please напишіть хоть кусочок кода, щоб зрозуміліше було як, що, де робиться.
Вказівник на об'єкт у векторі це типу так?
vector<Car*>vek
І чим ефективні динамічні об'єкти?
А є якісь функції для сортування?
Знайдені повідомлення: з 61 по 80 з 101