I have a situation where map elements will be inserted all throughout a continuous loop. I know that there's no reserve function for maps like there are for vectors, but I know exactly the maximum amount of elements there will be in the map, I just don't know the value of each individual map.
Would it be possible if I just inserted the maximum amount of elements in the beginning of the program, and change the value of each/any individual element later on? For example:
std::map<int, std::string> map; //There will be a maximum of 200 elements in this map for (int i = 0; i < 200; i++) { map[i]; }
//Change the value of individual elements as I wish
Is it more efficient than initializing on declaration if initializing will be done often?
I am writing a program that minicks the library reserve system at my university and the problem is that when i run the code and reserve a room i am to keep track of the amount of times the room has been visited. To do this i set up a array roomcount and I count up by one every time i assign a room, The bug lies with when you reserve a room, then run option three, it will show that the previous visits is at 1, which is good. but then if i leave the room(option 2) and then run option 3 again to view the status of the rooms, the previous visits is set back to 0, which is not good. Ive been staring at this for hours and asked numerous people by we can't seem to figure out why the roomcount variable resets.
#include <stdio.h> #include <math.h> int main (void) { int choice; int roomchoice; int i = 0;
The assignment is to read a data file with a list of names, capitalize the first character of the first and last name by making an array and subtracting 32 from the character, and then outputting the names onto the screen. So far I have:
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("Names.txt");
[Code] ....
this allows me to read a name, and capitalize the first letter of the first name. How to capitalize the first letter of the last name.
I want to do this but do not know exactly how to do it:
Create an array of space ship objects and write a program that continually updates their positions until they all go off the screen. Assume that the size of the screen is 1024 pixels by 768 pixels.
I have begun to write a bit code but the problem is when you want to see the space ship moving step for step on the screen I do not know how to create the ship visible so you can see it is moving. Here's my code so far
Code: #include <iostream> using namespace std; struct SpaceShips { int x_coordinate; int y_coordinate;
I used pointer(or is it not?) to make it one part only alphabets and the other one digits. The coding, calculate_charges.c and the open file, customer.txt are attached at the end of the post.
Code:
#include <stdio.h> #include <string.h> #define SIZE 3 void trimback(char input[], int strnameindex); void trimfrnt(char input[], int strnameindex); }
What would the worst, average and best case space complexity be for a data structure of type map<string, vector<int> > in big O notation? I'm parsing through a document and storing each word as a key and im attaching an associated int (in a vector) to it as the value.
I want the user to be able to enter a command then a character, like for example: push r....I want the command to be stored in the array command, and the character to be stored in the variable c.
Now I wonder what the best way to get rid of the space is, using scanf or getchar (see below for code, only thing that I changed between the 2 versions is the statement before the comment "get rid of space")? Or maybe it doesnt matter?
Code:
include <stdio.h> #define MAX 200 void push(char c); // Puts a new element last in queue char pop(void); // Gets the first element in queue static char s[MAX]; }
I am allocating space only for two characters but it fits all of them, if you run this it will print the whole string literal "hello my friend". How is that possible?
I am using gcc 4.6.3., I know about strncpy().
#include<iostream> #include<cstring> using namespace std; int main(){ char* str = new char[2]; strcpy(str, "hello my friend"); cout << str << endl; return 0; }
how can I read some strings that contains spaces and put them in a vector of strings, using the push_back() function?
I have a collection of functions, for example: [multiply_by_forty two, add_by_five]. All I want to do is to store the strings like: multiply_by, add_by in a vector of strings, and the arguments:forty two, five etc in another vector of strings, but with spaces. The function convert() converts written numbers to numbers (for ex the output of covert("forty two")is 42;)
I have a problem, caught the last line of a file and do the division of the same variables in, date, time and value, but the file contains excess spaces in all lines and the date is coming with cuts, example 24/4/2014 appears 4/4/2014. How can I remove the space in the string without affecting the division of the line?
if (file) { std::string line = getLastLine(file); std::istringstream iss(line); getline(iss, date, ' '); getline(iss, time, ' '); getline(iss, t); //variavel para temperatura em string } else { std::cout << "error file ";
I'm really new to SDL and i've been trying to make a spaceshooter game, so far I've got a ship you can move around in using the arrow key (square shaped) and a scrolling background. I'm up to making my ship fire bullets at the moment and it's really not working, i've finished lazy foo's tutorials on SDL and there's nothing on this.
Here's my code
#include "SDL.h" #include "SDL_image.h" #include "SDL_mixer.h" #include "SDL_ttf.h" #include <string> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480;
I have created a prompt which prompts the user for an integer and I have set up a loop to check for if it is an integer or not. My "bug" is that a user can enter an "integer" and "space" and "enter" and it does not give any error and assumes that "All is FINE!". I have gotten the value from the ascii table of 'SPACE' and put it as a check in my parameter of while, but it does not work.
Here is my code:
int x, y, boolean, i; char buff[256]; printf("Enter the first integer value: "); scanf("%s", buff); i = 0; boolean = 0; //initializing our boolean var that will eventually decide if we have an error or not
#include<iostream> #include<string> using namespace std; string strip(string message) { //removing elements that is not A-Z or a-z string stripped; for(int x = 0; x < message.length(); x++) {
[code]....
The output in my code shows like this:
[e]Encipher [d]decipher [x] Exit Enter choice: e Enter message to Encrypted: i love you Enter Key: love
The encrypted message is: tzjzpmjy
Back to main menu? (y/n):
However , I want the output to display the spaces as the user's input.
ex. The encrypted message is: t zjzp mjy.
This is the same as i love you in the user's input.
how to carry out the conversions. The assignment is the normal hex to octal and Quart (base 4) via bit munipulation which I have worked out myself. However, I have been trying all day to figure out how to read in a string such as H1234, or O4567. How to parse the input I can handle the remainder myself. I'm just stuck and I've tried for hours.
I was just going through Radix Sort algorithm. Though I got the logic and the concept used in it, I am still pretty much confused about the space complexity being O(k+n) for this algorithm. (where, k is the max no of digits in a number, and n is the no. of inputs to be sorted).
I'm planning to make a C++ project using a 3D library to visualise space and planets, maybe using raytracing. I wanted to know which library is the most appropriate to achieve it.