Can anyone point out what's wrong with my code? I tried swapping the vector's element to another element of the vector itself using std::swap(). It worked, the member class of each other have swapped but the problem is that in the left image the Blue Bar did not swap with the Red Bar.
Main.cpp
int main()
{
unsigned int width = 800;
unsigned int height = 600;
sf::RenderWindow window(sf::VideoMode(width, height), "Algorithm Visualizer");
std::vector<Bar> bars;
for (size_t i = 0; i < 10; i++)
{
Bar bar(sf::Vector2f(5, i + 10), sf::Vector2f(100 + i * 10, 300));
bars.push_back(bar);
}
std::cout << "Blue 1 height: " << bars[1].getHeight() << std::endl;
std::cout << "Blue 1 pos X: " << bars[1].getPos().x << std::endl;
std::cout << "Red 9 height: " << bars[9].getHeight() << std::endl;
std::cout << "Red 9 pos X: " << bars[9].getPos().x << std::endl;
bars[1].setColor(sf::Color::Blue);
bars[9].setColor(sf::Color::Red);
std::swap(bars[1], bars[9]);
std::cout << "After swapping new value: " << std::endl;
std::cout << "Blue 1 height: " << bars[1].getHeight() << std::endl;
std::cout << "Blue 1 pos X: " << bars[1].getPos().x << std::endl;
std::cout << "Red 9 height: " << bars[9].getHeight() << std::endl;
std::cout << "Red 9 pos X: " << bars[9].getPos().x << std::endl;
Bar.cpp
Bar::Bar(sf::Vector2f size, sf::Vector2f pos)
{
rect.setSize(size);
rect.setOrigin(rect.getOrigin().x, size.y);
rect.setPosition(pos);
}
Bar::~Bar()
{
}
void Bar::setColor(sf::Color color)
{
rect.setFillColor(color);
}
void Bar::setProperty(ShapeProperty property)
{
rect.setFillColor(property.fill_);
rect.setOutlineColor(property.outline_);
rect.setOutlineThickness(property.thickness_);
}
void Bar::setPosition(sf::Vector2f pos)
{
rect.setPosition(pos);
}
void Bar::setSize(sf::Vector2f size)
{
rect.setOrigin(rect.getOrigin().x, size.y);
rect.setSize(size);
}
void Bar::draw(sf::RenderWindow & window)
{
window.draw(rect);
}
void Bar::update()
{
}
int Bar::getWidth()
{
return rect.getGlobalBounds().width;
}
int Bar::getHeight()
{
return rect.getGlobalBounds().height;
}
sf::Vector2f Bar::getPos()
{
return rect.getPosition();
}