CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    array manipulation

    i have an array of strings

    how do i take the last two elements, and insert them in positions [0] and [1] of the array respectively, and the original elements two places up?

    thanks.
    With sufficient thrust, pigs fly just fine. However, this is not
    necessarily a good idea. It is hard to be sure where they are going to
    land, and it could be dangerous sitting under them as they fly
    overhead. -- RFC 1925

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: array manipulation

    If you are talking about C arrays, you need to move all the elements up two positions, one by one (starting on the top of course).

    If you are not bound to use C arrays, use a container that allows inserting and removing on both sides, e.g. a deque or a list.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: array manipulation

    I assume that you want to remove the last two elements
    and move them to the beginning. That is (for an int example) :

    0 1 2 3 4 5 6

    change to

    5 6 0 1 2 3 4

    if so, you might be able to use std::rotate in <algorithm>, but it
    depends on what your "array of strings" looks like.

  4. #4
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    Re: array manipulation

    deque looks promising

  5. #5
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    Re: array manipulation

    ok, currently i have
    Code:
    				playerList.push_front(playerList.back());
    				playerList.pop_back();
    				playerList.push_front(playerList.back());
    				playerList.pop_back();
    to achieve what i asked originally. Is there a more efficient way of using deque todo this?
    With sufficient thrust, pigs fly just fine. However, this is not
    necessarily a good idea. It is hard to be sure where they are going to
    land, and it could be dangerous sitting under them as they fly
    overhead. -- RFC 1925

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: array manipulation

    It may be possible to write something more efficient, but that one is already efficient.
    Complexity is O(1).
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    Re: array manipulation

    what would be the simplest way of seeing if a deque contains a particular element?
    With sufficient thrust, pigs fly just fine. However, this is not
    necessarily a good idea. It is hard to be sure where they are going to
    land, and it could be dangerous sitting under them as they fly
    overhead. -- RFC 1925

  8. #8
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    Re: array manipulation

    there an easier way then this?
    Code:
    bool inList(deque<string>list, string str)
    {
    	for(int i = 0; i < list.size(); i++)
    		if(list[i] == str)
    			return true;
    	return false;
    }
    With sufficient thrust, pigs fly just fine. However, this is not
    necessarily a good idea. It is hard to be sure where they are going to
    land, and it could be dangerous sitting under them as they fly
    overhead. -- RFC 1925

  9. #9
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: array manipulation

    You can use the find algorithm:
    Code:
    return (find(list.begin(), list.end(), str) != list.end());

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured