C++ :: Move Elements From One Container To Another Without Loop
Feb 17, 2012
Is there an algorithm in the STL to move elements similar to how std::copy works? I have read various places that the new C++ standard has a move algorithm. Unfortunately the compiler I use (g++ (GCC) 4.2.0) does not support any C++0x updates.
I have a std::deque that I want to move a range from into an array. I am currently using something like this where
data_array is an unsigned char pointer to a buffer being passed in by the caller
dataQ is a std::deque<unsigned char> that is a member variable maintained within the class
Code:
for (int i = 0; i < numberBytesRequested; ++i) {
data_array[i] = dataQ.front();
dataQ.pop_front();
}
I'm concerned that executing this loop over and over again is going to be very inefficient.
View 8 Replies
ADVERTISEMENT
Jun 29, 2014
Acyclic visitor pattern used here to count the number of elements of a certain type in a container.
struct A {
struct Visitor {
virtual ~Visitor() = default;
};
virtual void accept (A::Visitor&) = 0;
[Code] ....
Give the correct:
count = 3
But it is scrappy. The visitor classes had to be placed outside the classes they belonged to, and CountB lost its template because of that. Also, it is very awkward that I have to construct a "reverse hierarchy" for the Visitor classes (which means that this has to bechanged if I ever change the hierarchy for A, B, C, ...). How to improve my solution (using acyclic visitor pattern)? Forward declaring nested classes is not possible in c++.
View 1 Replies
View Related
May 13, 2014
I want to find all instances of a substring mysub in array container myarr and replace each occurrence of mysub with empty string " ". To do that, I'd like to use for loop with search algorithm.
Code below:
p=array iterator
for ( p=search(myarr.cbegin(),myarr.end(), mysub.begin(),mysub.end();
p!=myarr.end();
p=search(p,myarr.end(),mysub.begin(),mysub.end() ); {
[Code] ......
View 2 Replies
View Related
Mar 7, 2013
I am trying to get this code eventually to read in a maze file to move the smiley face around in. But right now my current snag is the yes or no to enter the for loop.
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
int main() {
int name;
char ans;
[Code] .....
View 3 Replies
View Related
Sep 14, 2014
I have a program that consist of a postfix calculator for a class assigment, my program is basically done, however I am missing how to loop through the stack and print each element of it. My output is suppose to look something like this:
Token: 4+5 (my token would be the infix expression that the user inputs)
output:45+ (my output would be the postfix expression)
Stack(bottom to top) :empty + + empty (This is suppose to be the process of the stack, but I do not know how to get this values to print out)
and I already have the token and output part, however I do not know how could I get the stack values, in order to print them out.
My program is the following:
#include <sstream>
#include<iostream>
#include<cstring>
#include<iomanip>
#include <vector>
#include <math.h>
#include <windows.h>
using namespace std;
[code].....
how to proceed with printing the stack so that it looks like my example output?
View 4 Replies
View Related
Mar 3, 2013
How can I move a character around a 2D map? After some research and a bunch of work I made a function for movement:
unsigned int gamespeed = 100;
unsigned int stage = 1;
void controls()
{
[Code]....
Maps are stored in a different .cpp file
So this code works, but is complicated, ugly and evil (I have to make a pointer to the first map and change the pointer to the next map every time the user reaches the exit, without the pointer this code is, of course, incompatible). How can I reduce this code to be less evil/ugly or at least smaller?
Also it would be nice if the user could move around with arrows as well as with WASD
View 5 Replies
View Related
Feb 24, 2014
After I enter in the 8 digit account number the program just stops and I can't find where the logical error is
Code: #include <iostream>
#include <iomanip>
#include <string>
using namespace std;
bool validateLength(string);
bool validateDigit(string);
void calculateA(string);
void calculateB(string);
void calculateC(string);
bool validateService(string);
[code]......
View 1 Replies
View Related
Jan 27, 2015
I want to move a character in a 2D array This Character should move vertically in a 2D Array To exemplify it for Exam in Snake Game A character automatically moves Please Write a example code that works
View 4 Replies
View Related
Apr 20, 2013
Having trouble getting my square to move to the left my code and instructions of what i am suppose to do is below. No sure how to move my square or if I am even going in the right direction as to writing code to do so. Note that it is only part of my code ( part 2 of project)
Part 2:
Write a graphical application that draws a square and then moves it.
Get the x and y coordinates for the top left corner of the square from the user using the get_int() member function of cwin. Get the length of a side of the square from the user using get_int() as well. Now draw the square to cwin according to the user input.Ask the user how many units to the left they want to move it using get_int() again. Then move the square, clear the screen, and draw it again.
// Part 2 //
/* command output to declare the x,y value and 1 side length of a square through user interaction( Has user input intergers) */
int x_value = cwin.get_int("What is the x_value of the top left of the square?");
int y_value = cwin.get_int("What is the y_value of the top left of the square?");
int side_length = cwin.get_int("Input the length for one side of the square:");
/* Data type for the 4 corners of the square */
Point e;
Point f;
[Code]...
View 2 Replies
View Related
Mar 27, 2014
So I'm writing an RPG and I'm in need of an inventory system. Of course as an relatively old member of the forum I know best than just come here and ask so I've already researched quite a bit and I've formulated this idea.
I've kind of conceptualized it like so: I'll have some sort of STL container of a unique_ptr of my base item class. There will be derived item classes. Taking advantage of polymorphism I can then call the new Derivedclass when inserting it in the STL container.
My questions are: What STL container should be used for the inventory(fixed sized)?
View 2 Replies
View Related
Sep 22, 2013
What is container class? what its advantage ?
View 2 Replies
View Related
Sep 16, 2012
I have a pile of data, which i need to access frequently and fast. One entry consists of two unsigned ints, let`s call them source and destination.
One source can have several destinations, but this rarely ever happens. Several sources can have the same destination - this happens more frequently, but still scarcely.
Once the data is set up, it rarely ever changes - yet it should be possible to add or remove entries.
I now need to find all sources for a given destination, or all destinations for a given source.
The question: which stl container to choose?
I have tried a multimap, using the source as key. This works good for finding all d for a given s, but is inefficient in the other direction.
Do you think it would be more efficient to have two multimaps, sorted respectively by source and destination?
View 1 Replies
View Related
Mar 5, 2013
My objectives for this program is to open and read a file "dict.txt" which has the following data:
4
dog
pepper
marker
teapot
It needs to rotate the inner letters one to the right so you'd end up with "mrkear" etc, and then write those words into a new file "scramble.txt" one per line.I'm having a lot of problems understanding the program. Compiling is giving me a lot of warnings/errors.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXW 10 //max number of words in file
#define MAXS 81 //max size of string
}
[code]....
View 2 Replies
View Related
Dec 6, 2013
I am doing a checkers program in C and were not allowed to use standard C99. Things are going relatively well so far. I have 2d array that acts as a matrix for my board and have ways to check if a space is empty and if it is occupied by a specific players chip.
Right now I have a giant messy method that I will split up sooner or later but Im just trying to understand and build the logic. It looks like this:
Code:
void isValidMove(int origin, int dest, int player) {
int rowDiff = abs(origin%8 - dest%8);
int possibleMoveLeftUp = origin - 9;
int possibleMoveLeftDown = origin + 9;
int possibleMoveRightUp = origin - 7;
[Code] ....
So although its not pretty i think the logic is sound. I can tell whether or not a player is attempting to make a move or a jump. I haven't implemented make_move() yet but thats no problems. My biggest deal is that I need to find a way to tell the user he has to jump if its available. I feel like this is a very difficult task for me to grasp and is more about algorithm logic and math then the C language which is what the course is for.
So, how I could loop through all of a specific players chips and see if that player has an available jump. Also if he does then the jump has to be taken and then checked again as if there is another legal jump then it needs to be taken as well until their are no jumps left. Of course there could be 2 routes available, but I think I could deal with that if I just could come up with a reasonable way of checking....
View 2 Replies
View Related
Feb 7, 2013
How can I move data from one file to the another file without using pointers.
View 7 Replies
View Related
Jan 13, 2015
how I want my sprite movement to work: if my sprite is faced upwards and i press W, it will move up. If my sprite is faced to the right and i press W, it will go right. etc. // It doesn't work like that right now and how to do it.The sprite's rotation works fine.
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
playerSprite.rotate(-0.08 * dt);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::E)) {
playerSprite.rotate(0.08 * dt);
[code]....
My question is how do I move the character depending on the rotation?
Also, you might see the "if(havePlayersCollided == false) {playerSprite.move(yadiyada)}" . yes i dont need the haveplayerscollided function because it doesnt work the way I want it to. I might make a thread for it in the future, but right now I need to get the rotation movement fixed before I move onto the collision detection between players.
View 7 Replies
View Related
May 29, 2014
I am working on a bonus question for school. By using #include <iostream> and #include <string>, I need the user to input a sentence getline(cin,sentence). Move the first word of that sentence to the end.
example:
Enter a line of text.
MAC101 is the class
rephrased that line to read:
is the class MAC101
View 12 Replies
View Related
Dec 3, 2013
How to move a character (an arrow or any other character) on screen using keyboard? I'm actually a beginner and have only wrote simple (starter's) programs in C++. It would be great without using any external library which is not included in C++ by default (in other words, using plain C++).
View 8 Replies
View Related
Apr 3, 2014
I was wondering if there is way to convert a C# class to its own project and it automatically keeps its references.
View 3 Replies
View Related
Apr 10, 2014
How to move a rectangle in TabPage ?
View 2 Replies
View Related
Apr 29, 2013
What c++ code can be used to make an 'X' move through a maze. I have some code, but I'm not sure where to go from there. I have divided the program into three files, A header file, a main file and a .cpp implementation file.
In my implementation file I have:
#include "Maze.h"
Maze::Maze() {
} void Maze::mazeTraversal(char maze[][COLS], int row, int col, int direction) {
enum Direction {DOWN, RIGHT, UP, LEFT};
switch(option)
[Code] ....
View 1 Replies
View Related
Feb 13, 2013
i draw some object (polygon , triangle , etc.. ) and i want to move them from side to side automatically and from keyboard ...
View 1 Replies
View Related
May 17, 2012
I have a data-bound ComboBox and I populate a DataSet with the values returned from a SQL query.
public DataSet CompanyArray(DataSet dataSet) {
SqlConnection connection = InstancesTest.IDataInterface.DefineConnection();
SqlCommand command = new SqlCommand();
command.CommandText = "usp_CompanyArray";
command.CommandTimeout = 20000;
[code]....
All this works fine. The reason I populated it in an array was so that I could sort the items alphabetically. I know I could just do an "order by" in SQL but I chose this route.
What I'm after is putting a "Please Select" at index 0 in the ComboBox. How can I move this value to the top of the list, without reordering the sorted list?And I don't really want to insert a "Please Select" as a first row in a database table.
View 2 Replies
View Related
Mar 26, 2012
How do I tell c++ to move the mouse in place x?
for example, I run a c++ program and the mouse moves to the top left corner of my screen.
How do I go about doing something like that?
View 2 Replies
View Related
May 5, 2013
I am unable to understand how a move constructor works in this example of code. If someone could break down the process of what is taking place and explain to me on why to use a move constructor.
Code:
class MyString {
MyString(MyString&& MoveSource) {
if( MoveSource.Buffer != NULL ) {
Buffer = MoveSource.Buffer; // take ownership i.e. 'move'
MoveSource.Buffer = NULL; // set the move source to NULL i.e. free it
}
}
};
Example from "SamsTeachYourself: C++ in One Hour a Day"
View 1 Replies
View Related
Mar 6, 2013
I am trying to come up with a way to make use of a "multilevel dynamic" container. I am parsing a file to grab some pieces of data. Lets say the first field of data I find I push into an array. At the same time lets I wish to create 2 cascaded sublevels. So an element in Modules is a pointer to the Types vector associated with that module and each element in Types is a pointer to a vector of Data. This concept should be similar to memory paging.
/*
Modules Types Data
____ _____ ______
|____|------> |_____| ---------->|______|
|____| |_____| |______|
|____| |_____| |______|
|____|
|____|
|____| _____ ______
|____|------> |_____|----------->|______|
|____| |_____| |______|
*/
Obviously this becomes very hair quickly so it is obvious that I need to dynamically create and destroy vectors (if I do it this way). Should I just create pointers using the new operator?
Here is some of my code if it is even worthwhile to read:
class Parser {
//storage arrays
vector <string> modules;
vector <string> terminal_type;
vector <string> tag_type;
vector <string> data;
[Code] ....
parse this stuff and nicely organize it.
If you take the first one, "max_logvar" is a module so everything between < and > is associated with that module.
symb is unimportant for now.
then "proterm" is a "module type" so then module now needs a module type container but I may have more than one of those.
so then I break it down by "Input" and "Output" where each of those can have the integer values (just in an array where each position will be set) that are in the fields to the right.
View 1 Replies
View Related