473,795 Members | 3,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

move element in vector

(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 CircularContain er::insert(cons t 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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}

m_elements = temp_array;
}
else
{
m_elements.inse rt(m_elements.b egin(), s);

if(m_elements.s ize() > 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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}
}

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
Jul 22 '05 #1
7 12634
"William Payne" <mi************ **@student.liu. se> wrote in message
news:ch******** **@news.island. liu.se...
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.


Don't use a vector. Use a (de)queue or a list.

-Mike
Jul 22 '05 #2
On 8/30/2004 11:59 PM, William Payne wrote:
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.


It seems to be a kind of Most Recently Used solution ;-)
I have my own one and I developed that using double linked list.
I think dequeue will work well too.

Greets

--

Mateusz £oskot
mateusz at loskot dot net
Jul 22 '05 #3
William Payne wrote:
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.


That depends on the container...
IMO you could use rotate:

#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
const std::size_t max_size = 5;
std::vector<int > v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);

int newItem = 2;

std::vector<int >::iterator found =
std::find(v.beg in(), v.end(), newItem);

if(found == v.end())
{
if(v.size() >= max_size)
v.pop_back();
v.insert(v.begi n(), newItem);
}
else
{
std::rotate(v.b egin(), found, found + 1);
}

std::copy(v.beg in(), v.end(),
std::ostream_it erator<int>(std ::cout, "\n"));

return 0;
}
--
Regards,
Tobias
Jul 22 '05 #4
William Payne wrote:
(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 CircularContain er::insert(cons t 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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}

m_elements = temp_array;
}
else
{
m_elements.inse rt(m_elements.b egin(), s);

if(m_elements.s ize() > 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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}
}

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.

What about using std::list<> instead of std::vector<>. Here is a version
that is templated on a container type, so that you can do experiments as to
see what works best:
#include <algorithm>

template < typename T, template < typename S > class container >
class SelfOrganizingC ache {
public:
typedef T value_type;
typedef container< value_type > sequence_type;
typedef typename sequence_type:: size_type size_type;
typedef typename sequence_type:: difference_type difference_type ;
typedef typename sequence_type:: const_iterator const_iterator;
typedef typename sequence_type:: const_reverse_i terator
const_reverse_i terator;

private:

sequence_type the_sequence;
size_type the_size;
size_type the_capacity;

void push_item ( const value_type & item ) {
the_sequence.in sert( the_sequence.be gin(), item );
++ the_size;
}

void pop ( void ) {
the_sequence.po p_back();
--the_size;
}

public:

SelfOrganizingC ache ( size_type capacity )
: the_sequence ()
, the_size ( 0 )
, the_capacity ( capacity )
{}

~SelfOrganizing Cache ( void ) {}

void push ( const value_type & item ) {
typename sequence_type:: iterator item_pos
= std::find( the_sequence.be gin(), the_sequence.en d(), item );
if ( item_pos != the_sequence.en d() ) {
the_sequence.er ase( item_pos );
-- the_size;
}
push_item( item );
if ( the_size > the_capacity ) {
pop();
}
}

void resize ( size_type capacity ) {
the_capacity = capacity;
while ( the_size > the_capacity ) {
pop();
}
}

size_type size ( void ) {
return( the_size );
}

size_type capacity ( void ) {
return( the_capacity );
}

const_iterator begin ( void ) const {
return( the_sequence.be gin() );
}

const_iterator end ( void ) const {
return( the_sequence.en d() );
}

const_reverse_i terator rbegin ( void ) const {
return( the_sequence.rb egin() );
}

const_reverse_i terator rend ( void ) const {
return( the_sequence.re nd() );
}

}; // SelfOrganizingC ache

/*
Testing push:
*/

class RandomNumberGen erator {
public:

RandomNumberGen erator ( unsigned long int seed )
{
std::srand( seed );
}

RandomNumberGen erator ( void )
{}

unsigned long lower ( void ) const {
return ( 0 );
}

unsigned long upper ( void ) const {
return ( RAND_MAX );
}

unsigned long operator() ( void ) {
return( std::rand() );
}

unsigned long int operator() ( unsigned long int bound ) {
unsigned long int_width = RAND_MAX / bound;
unsigned long max_valid = int_width * bound;
unsigned long seed;
do {
seed = std::rand();
} while ( seed >= max_valid );
return( seed / int_width );
}

}; // RandomNumberGen erator

#include <iostream>
#include <list> // reccommended
#include <vector>
#include <deque>

int main ( void ) {
typedef SelfOrganizingC ache< unsigned long, std::vector > Cache;
Cache c ( 10 );
RandomNumberGen erator R ( 12344 );
for ( unsigned long i = 0; i < 100; ++i ) {
unsigned long l = R( 15 );
std::cout << "pushing " << l << " :";
c.push( l );
for ( Cache::const_it erator iter = c.begin();
iter != c.end();
++ iter ) {
std::cout << " " << *iter;
}
std::cout << "\n";
}
}

Best

Kai-Uwe Bux

Jul 22 '05 #5
In article <ch**********@n ews.island.liu. se>,
"William Payne" <mi************ **@student.liu. se> wrote:
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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}
}

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.


<nod> You only need to make temporary space for the found string, well
actually not even that since it is already stored in s. You don't need
a temporary vector. How 'bout this:

if(itr != m_elements.end( ))
*--std::copy_backw ard(m_elements. begin(), itr, itr+1) = s;
else
...

Saves the temp vector. And saves disturbing at all any strings beyond
itr. And should also work fine if you switch to list or deque. If you
switch to list, you might want to investigate splice instead of
copy_backward as it would probably increase performance. But it is not
a given that you should switch. Measure first.

If by any chance you use Metrowerks CodeWarrior, Metrowerks::cde que is
another excellent container for this application (it is a contiguous
memory circular buffer). It would optimize the push_front/pop_back
branch of your logic.

-Howard
Jul 22 '05 #6

"William Payne" <mi************ **@student.liu. se> wrote in message
news:ch******** **@news.island. liu.se...
(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 CircularContain er::insert(cons t 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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}

m_elements = temp_array;
}
else
{
m_elements.inse rt(m_elements.b egin(), s);

if(m_elements.s ize() > 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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}
}

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

Thanks for all the replies. I ended up trying the following code:

vector<string>: :iterator itr =
find(m_elements .begin(), m_elements.end( ), s);

if(itr != m_elements.end( ) && itr != m_elements.begi n())
{
/* Copy element to the front. */
m_elements.inse rt(m_elements.b egin(), *itr);

/* Erase the the element at the old position. */
m_elements.eras e(itr);
}

Then I tried inserting the string "three" when m_elements contained
(ascending index order):
five
four
three
two

I expected the result to be:
three
five
four
two

But I got:
three
five
three
two

Why?? I don't understand...cl early erase() isn't working as I thought it
would work.

/ WP
Jul 22 '05 #7

"William Payne" <mi************ **@student.liu. se> wrote in message
news:ch******** **@news.island. liu.se...

"William Payne" <mi************ **@student.liu. se> wrote in message
news:ch******** **@news.island. liu.se...
(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 CircularContain er::insert(cons t 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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}

m_elements = temp_array;
}
else
{
m_elements.inse rt(m_elements.b egin(), s);

if(m_elements.s ize() > 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[i] != *itr)
{
temp_array.push _back(m_element s[i]);
}
}
}

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

Thanks for all the replies. I ended up trying the following code:

vector<string>: :iterator itr =
find(m_elements .begin(), m_elements.end( ), s);

if(itr != m_elements.end( ) && itr != m_elements.begi n())
{
/* Copy element to the front. */
m_elements.inse rt(m_elements.b egin(), *itr);

/* Erase the the element at the old position. */
m_elements.eras e(itr);
}

Then I tried inserting the string "three" when m_elements contained
(ascending index order):
five
four
three
two

I expected the result to be:
three
five
four
two

But I got:
three
five
three
two

Why?? I don't understand...cl early erase() isn't working as I thought it
would work.

/ WP


Bah, never mind! I forgot to increment the iterator after performing the
insertion. Now it works great.

/ WP
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
34399
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if this element's value happens to be 0, the conditional will return false, even though the element actually exists. Any ideas? Can you check if the element == null?
3
1997
by: Jonathan | last post by:
Hey again everyone! I have another question for you guys. I am trying to erase a certain vector element based on what number the user selects. Here is what I did: case 'b' : { cout << "Please enter note number to clear: "; cin >> clear_note; clear_note = clear_note - 1; vec.erase(clear_note);
11
2907
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
2
4742
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently remove a random element by swapping it with the last element and then just using pop_back? Does erase do this internally? Or does erase do the element shift to fill in the gap?
2
2051
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the char array strarr holds garbage characters. For some reason, every time the strcpy function is called to copy the string in temp to the array, it fills the 0th element in the strarr array with garbage characters. Can someone try to compile this...
5
12087
by: ma740988 | last post by:
For starters, Happy New Year to all!! I created a vector of pairs where pair first is a primitive and pair second is a vector of ints. So now: # include <iostream> # include <vector>
5
3546
by: levent | last post by:
Hi, Is it possible (with STL) to move the contents of a vector into a list? Obviously this would be a linear complexity operation (number of elements in vector), but is it possible to do this w/o copy constructing every element into list and destructing the vector contents?
35
3054
by: Frederick Gotham | last post by:
(Before I begin, please don't suggest to me to use "std::vector" rather than actual arrays.) I understand that an object can have resources (e.g. dynamically allocated memory), and so we have to copy an object properly via copy- construction rather than using memcpy. However, is it okay to move an object manually (i.e. by using memcpy or memmove)?
7
4354
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.