C++ :: Construct Maps Initialization
May 5, 2013
I've a problem with the map construct. I wrote this class in which there are maps:
class Features {
private:
map<const string,GenericFeatureContainer*> featuremap;
map<const string,GenericFeatureContainer*>::iterator it;
int size;
public:
}
And I have the following constructor:
Features::Features() {
map<NULL,NULL> featuremap;
it=NULL;
size=0;
}
Is that correct? otherwise what is the correct syntax?
View 2 Replies
ADVERTISEMENT
Nov 26, 2014
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?
View 2 Replies
View Related
Sep 19, 2014
Currently im creating a simple phone directory. I am having a problem when searching the vector. It only lets me search for the exact key in the directory when I need to to search for strings that are close to the name. For example when I search "car" it will state that its not in the directory when it should pull up Carr, Derek and Carr David.
#include <string>
#include <map>
#include <vector>
#include <ostream>
#include <iostream>
int main() {
std::map<std::string, std::vector <std::string> > directory;
[code]....
View 1 Replies
View Related
Dec 7, 2014
GameMaster.h
#includes ...
class GameMaster
{
[Code].....
I get the segmentation fault whenever it tries to insert the first player.. however whenever i do this :
void MakeSquad(char type, string name, int coordX, int coordY, char dir, int squad)
{
GameSpace::MapSquadType MapOfSquads;
MapOfSquads.insert(make_pair(name , Squad(squad)); // Squad( int ) just converts the int squad number to the Enum.
}
I do not get any errors or segmentation faults..
View 5 Replies
View Related
Dec 29, 2014
i have made this first level in this ascii game, and i realized that if i want to continue, i must rewrite the switch statement and write thousands of lines of code. how can i load a map from a txt file?
#include <iostream>
#include <windows.h>
using namespace std;
[Code].....
View 3 Replies
View Related
Feb 9, 2014
I have code working, but that is largely because a vector that I am using as part of a structure that is the first of two elements of a MAP (with the second being an integer that is assigned later) is serving in its original, purely vector form, to fill out a set of selections on a DOS screen (option 1, 2, 3, etc.).
I do use the MAP, because it is a simple means of referencing the integer value using a word that is part of the structure, and is probably appropriate, saving me a search through the vector of words and calling up the value at the same location in the vector of words in a vector of corresponding numerical values. I DO feel that what I'm doing to produce the selection list is something of a "cheat" because I do not know how to access the same data within the structure component of the MAP by simply using a similar numerical reference (e.g. "MAP[1].structure.word", or would that be "MAP.structure.word[1]" ????) to print out the words in the structure in the map as choices on the screen.
I don't want to change or expand the contents of the map, just access and print each one in a simple loop with a number from the loop counter to the left of each word.
View 4 Replies
View Related
Apr 4, 2013
Code:
if (IS_LEAP_YEAR(year))
const int days_per_month[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
else
const int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Is it ok to declare the array in this manner or is it bad?
And i have to ask the user for a date to enter in my program. So should I use scanf or should I store the date in a string and then use sscanf. I have to check for valid input for everything like day, month, year etc. I did it as below..
Code:
int assignments;
assignments = scanf("%d / %d / %d", &month, &day, &year);
fflush(stdin);
if (assignments != 3)
{
printf("Retry: ");
}
else
error checking.
View 9 Replies
View Related
Apr 26, 2014
This code i made divided a user input into characters and non character. The problem im having is that if a mixed sentence is created, such as 32B, it will only print the 'char string' and not the 'non char string'. But when the sentence is just non characters like 32 it will print the 'non-char string'. So essentially if a mixed sentences is created both of the strings won't be created or printed.(This is only a function by the way).
Code:
Count_chars( char Input[]) {
int i;
for(i = 0; Input[i]; i++) {
if((Input[i] <= 122) &&(Input[i] >= 97)){
printf("Char %c ",Input[i]);
Char[i] = Input[i];
[Code]...
View 4 Replies
View Related
May 28, 2013
I am writing code, which reads an input file (280 MB) containing a list of words. I would like to compute
1) total # of words
2) total # of unique/distinct words
3) mean/median/mode of word count (Link)
I managed to get done with 1) and 2), but my program crashes for 3). I am not quite sure whether there are memory issues or some bug in the code.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
[Code] .....
View 2 Replies
View Related
Apr 13, 2014
This code i made, utilizing the type casting construct, isn't outputting what i wanted. The output for 'Dollars' and 'Cents' are returning '0' for both. instead all i want it to do is seperate the two. for example changing the float value of amount to an integer, giving a dollar value.
Code:
#include <stdio.h>
int main()
{
}
[code]....
View 1 Replies
View Related
Jun 3, 2013
How to construct a simple hash function ? When the program executes, it will ask for your name. Type your name and it will print out a "hash code" (a number) from that name.
View 2 Replies
View Related
Jun 27, 2014
have a problem with my code (I wish for answer with code). The conditions I have to grant:
- **Only** a pointer to an object must be saved in a **standard** class vector (e.g vector<ShapePtr>)
- Base class must be a polymorphic class (I call this class ShapePtr)
- I **must** have a deep copy process
- Constructor is **not** suppose to do a deep copy
- **clone()** is suppose to do the deep copy
Here is the code **provided** to me:
main.cpp
int main() {
typedef shared_ptr<Shape> ShapePtr;
vector<ShapePtr> shapevec;
[Code].....
View 1 Replies
View Related
Feb 26, 2015
If I have an integer variable like int a=9 then in the switch case If i write :
switch(a) {
case 4+a: printf("hii");
}
Then why is this statement a compile-time error that variables cannot be used inside a case statement why does the compiler not subtitutes the values in place of the variables.
View 1 Replies
View Related
Apr 12, 2014
This code i made is a cent converter from 5 to 95 cents. The problem i'm receiving is when the 'cents' function is sent back to the 'main' function it only prints one line. It seems to just print the first if construct that complies with the statement. Is there anyway i can have this function print multiple cent values? For example if 60 cents was entered it would only print '50c', and i want it to print '50c' and '10c' instead.
Code:
#include <stdio.h>
int x;
void check(int x)
{
if( x < 5)
printf("Less then 5 cannot be calculated
");
else if(x > 95)
[code]....
View 3 Replies
View Related
Mar 13, 2013
how to read characters from user and construct a sting and then extract part of it using substring?
View 2 Replies
View Related
Nov 5, 2013
I am trying to construct a program without a main function that outputs stars but at the same time outputs the number and a colon after the number but before the number of star. This is the coding i have so far.
void output_stars(int num){
if (num > 0){
cout << "*";
output_stars(num-1)
[Code] ....
The program is working the way it should i just can't figure out how to output the number and a colon. The main function is written as such:
output_stars(1);
output_stars(2);
output_stars(5);
output_stars(3);
output_stars(4);
output_stars(7);
How to get the numbers of the main function to show up with the stars of the previous function.
View 6 Replies
View Related
Nov 29, 2014
I'm trying to read a "pointer-based" maze .txt. The first letter in each row corresponds to the room letter...then the letters that follow are North node, East node, South node, and West node respectively. The asterisk indicates an empty room or not a valid option.
Here is what I have come up with, what is happening is after the file is parsed by read_maze it is calling my is_empty function indicating that there is no maze because it doesn't go into the else statement here.
I've attached a sample input file:
maze.txt (130bytes)
Number of downloads: 19
We can't assume the rooms will be in order alphabetically A - Z, We are expecting a maximum of 12 rooms and there is a space between each letter or asterisk.
void Maze::read_maze(string FileName){
string line;
ifstream inStream;
inStream.open(FileName.c_str());
int test = inStream.peek();
int i = 0;
if (!(inStream.fail())){
while (!inStream.eof() && test != EOF){
[code]....
View 5 Replies
View Related
Feb 26, 2015
I just wanted to ask the reason that why is the below code not checking the case -1 while working for the other case values.
#include<stdio.h>
#include<conio.h>
int main() {
int i=-1;
switch(i-2) {
[Code] ....
So why in the below code the case -1 doesnt run,when the value evaluated by the switch construct is actually a negative integer constant.
View 1 Replies
View Related
Apr 19, 2014
I need to construct Truncated Gaussian distribution in the range of (5-20 microns), specifyed by initial standard deviation.
Particularly i need input which comprises of minimum value(5 microns), maximum value(20 microns) and standard deviation of (5,10,20 etc..). In this regard i tried with Box-Muller but it doesn't fulfuil the goal.
View 2 Replies
View Related
Aug 22, 2013
I can't get my code to compile, i need to read in lines from a file and store them in variables. Then i have to construct instances of my class for how many lines there are in the file and take those variables into them.
I'm getting this error :
"a2.cpp:40: error: cannot convert `Employee' to `Employee*' in assignment"
#include<iostream>
#include<string>
#include<fstream>
void displayInfo();
using namespace std;
class Employee{
[Code] .....
View 1 Replies
View Related
Apr 27, 2013
Here is a function,which deletes the spaces of a string...
char *removespaces(char *s1) {
Code: char *s2=s1;
int i,j=0;
for (i = 0; i<strlen(s1); i++){
if (s1[i]!=' ') {
s2[j]=s1[i];
[Code] .....
why I have to initialize the pointer *s2 with the first element of the array s1...???If I don't initialize the pointer,or initialize it with something else,I get a segmentation fault...
View 10 Replies
View Related
Apr 28, 2013
I would like to initialize an arry containing function pointers with adresses of functions that have a variable number of arguments.
Below the code that works in principle. I would however get rid of the warning message during compilation pointing to the initialzation of the funtion pointers in the array. How do I need to cast the pointers to the functions ?
Code:
gcc func_ptr_init.c
func_ptr_init.c:28: warning: initialization from incompatible pointer type
func_ptr_init.c:32: warning: initialization from incompatible pointer type
Code:
#include<stdio.h>
unsigned char func1_ptr(unsigned int* if_desc, int* result_code) {
*if_desc = 1;
*result_code = 1;
return(0);
[Code] ....
View 8 Replies
View Related
Apr 4, 2014
In a book I'm reading, the author has the following struct:
struct VertexPos {
XMFLOAT3 pos;
};
Where XMFLOAT3 is a structure with x,y,z floating point values within. He declares and initializes an array as the following.
VertexPos vertices[] =
{
XMFLOAT3(0.5f, 0.5f, 0.5f),
XMFLOAT3(3.3f, 5.6f, 3.6f),
XMFLOAT3(-4.5f, 2.2f, 6.4f)
};
It doesn't make sense to me why this is working. The way my mind is thinking about this is that vertices is an array of type VertexPos, so then why can it be initialized with objects of type XMFLOAT3?
View 2 Replies
View Related
Nov 17, 2014
I have a class containing a map member that I want to initialize at declaration time. I know I can do it in the cpp file but I'm having a problem with the order of initialization (static initialization order fiasco).
My questions are:
Is it possible that the scenario in which, the Test's constructor's implementation and the map initialization instruction are in the same cpp file and constructor is called when the map is not initialized yet, could happen?
Is it possible to initialize the map in class like I did? I get these errors:
in-class initialization of static data member 'std::map<std::basic_string<char>, Test*> Test::a' of incomplete type
temporary of non-literal type 'std::map<std::basic_string<char>, Test*>' in a constant expression
If yes, does this initialization resolve the static initialization order fiasco?
class Test {
public:
static std::map<std::string, Test*> a = {};//this is an error
Test(std::string ID) {
[Code] ....
View 4 Replies
View Related
Jan 1, 2014
The below example is an initialization of a pointer array:
char *month_name(int n) {
static char *name[] = {
"Illegal month",
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (n < 1 || n > 12) ? name[0] : name[n];
}
Here we have an array of character pointers. The pointers are stored in name[i]. And they point to the characters of the i-th string, which are stored somewhere else in memory. But Is the character pointer pointing to the first character in the character string that is stored somewhere else in memory? I assume so because a string itself is an array of characters. And if that is the case, how does the pointer know what the last character should be? Does it just search for the null terminator?
View 2 Replies
View Related
Nov 16, 2014
I have a custom struct hierarchy that goes vaguely like this:
struct Base
{};
struct Derived1 : public Base {
int num;
[Code] .....
I'm using "Base" simply as an umbrella struct, so I can access any of the derived structs with a "base" reference(&).
The issue I'm having is, I have a class that has a data member that is a reference to the struct "Base" but, I'm getting an error that says my constructor for this class doesn't provide an initialiser for that data member.
I've tried intialising a derived object for the reference, like so:
MyClass:MyClass() {
Derived1 d1;
d1.num = 0;
mBaseRef = d1;
}
But, it doesn't work...
View 1 Replies
View Related