Skip to content

Commit

Permalink
Merge pull request #3158 from robotology/vector_erase
Browse files Browse the repository at this point in the history
yarp::sig::Vector::erase()
  • Loading branch information
randaz81 authored Jan 14, 2025
2 parents 61be8f3 + f0757e9 commit 7e1e41e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/libYARP_sig/src/yarp/sig/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,26 @@ class yarp::sig::VectorOf : public VectorBase
}

/**
* Resize the vector and initilize the element to a default value.
* Remove an element from the vector.
* @param pos iterator pointing at the element to remove
*/
void erase(iterator pos)
{
bytes.erase(pos);
}

/**
* Remove one or more elements from the vector.
* @param first iterator pointing at the first element to remove
* @param last iterator pointing at the last element to remove
*/
void erase(iterator first, iterator last)
{
bytes.erase(first, last);
}

/**
* Resize the vector and initialize the element to a default value.
* @param s the new size
* @param def the default value
*/
Expand Down Expand Up @@ -263,7 +282,7 @@ class yarp::sig::VectorOf : public VectorBase
/**
* @brief Construct a new element in the vector: size is changed
* @param args, arguments to be forwarded for constructing the new element.
* @return the reference to the new element contructed.
* @return the reference to the new element constructed.
*/
template<typename... _Args>
inline T& emplace_back(_Args&&... args)
Expand Down
15 changes: 14 additions & 1 deletion src/libYARP_sig/tests/VectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,20 @@ TEST_CASE("sig::VectorTest", "[yarp::sig]")
CHECK(v[2] == 3.0); // Checking data consistency
}

SECTION("Checking the the for range based")
SECTION("Checking the erase method")
{
Vector v{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
Vector vtest1{0.0, 1.0, 3.0, 4.0, 5.0, 6.0};
Vector vtest2{0.0, 1.0, 5.0, 6.0};

v.erase(v.begin() + 2);
CHECK(v == vtest1);

v.erase(v.begin() + 2, v.begin()+4);
CHECK(v == vtest2);
}

SECTION("Checking the for range based")
{
Vector v{0.0, 1.0, 2.0, 3.0, 4.0};
double i = 0.0;
Expand Down

0 comments on commit 7e1e41e

Please sign in to comment.