W
William Payne
(This post is related to "recent files menu"-post below. If I should have
kept this in that thread, I apologise.)
Hello, I have a function that adds a std::string to a std::vector. New
entries are added at the front (index 0) of the vector. If the vector
contains a certain amount of elements, the element at the back is removed
when a new one is added. If one tries to add a string already stored in the
vector, that string is supposed to move to the front, pushing everything
else before it down one notch. Here's my solution:
void CircularContainer::insert(const string& s)
{
vector<string>::const_iterator itr =
find(m_elements.begin(), m_elements.end(), s);
if(itr != m_elements.end())
{
vector<string> temp_array;
temp_array.push_back(*itr);
for(size_t i = 0; i < m_elements.size(); ++i)
{
if(m_elements != *itr)
{
temp_array.push_back(m_elements);
}
}
m_elements = temp_array;
}
else
{
m_elements.insert(m_elements.begin(), s);
if(m_elements.size() > m_max_size)
{
m_elements.pop_back();
}
}
}
My questions is about the code that deals with the case where the string was
already found in the vector:
if(itr != m_elements.end())
{
vector<string> temp_array;
temp_array.push_back(*itr);
for(size_t i = 0; i < m_elements.size(); ++i)
{
if(m_elements != *itr)
{
temp_array.push_back(m_elements);
}
}
}
Is there a better way to move the element to the top, maybe using some
function in the standard library I don't know about? Creating a completely
new vector and copying each element in the correct to that one seems so
brute-forceish.
/ WP
kept this in that thread, I apologise.)
Hello, I have a function that adds a std::string to a std::vector. New
entries are added at the front (index 0) of the vector. If the vector
contains a certain amount of elements, the element at the back is removed
when a new one is added. If one tries to add a string already stored in the
vector, that string is supposed to move to the front, pushing everything
else before it down one notch. Here's my solution:
void CircularContainer::insert(const string& s)
{
vector<string>::const_iterator itr =
find(m_elements.begin(), m_elements.end(), s);
if(itr != m_elements.end())
{
vector<string> temp_array;
temp_array.push_back(*itr);
for(size_t i = 0; i < m_elements.size(); ++i)
{
if(m_elements != *itr)
{
temp_array.push_back(m_elements);
}
}
m_elements = temp_array;
}
else
{
m_elements.insert(m_elements.begin(), s);
if(m_elements.size() > m_max_size)
{
m_elements.pop_back();
}
}
}
My questions is about the code that deals with the case where the string was
already found in the vector:
if(itr != m_elements.end())
{
vector<string> temp_array;
temp_array.push_back(*itr);
for(size_t i = 0; i < m_elements.size(); ++i)
{
if(m_elements != *itr)
{
temp_array.push_back(m_elements);
}
}
}
Is there a better way to move the element to the top, maybe using some
function in the standard library I don't know about? Creating a completely
new vector and copying each element in the correct to that one seems so
brute-forceish.
/ WP