C++ :: Initializing / Filling Container With Sample Of Another
May 22, 2014
I have a vector that I want to use to source another vector, something like copy_if, but without the need to allocate space first:
bool is_odd( const int x ){ return x % 2 == 1; }
std::vector< int > numbers = { 1, 2, 3, 4, 5, 6 };
std::vector< int > odd_numbers;
std::sweet_function( numbers.begin(), numbers.end(), odd_numbers, is_odd );
// odd_numbers is { 1, 3, 5 }
Even better might be a std::transform_if
View 5 Replies
ADVERTISEMENT
Apr 9, 2014
I'm writing a code using least mean square to fit a sample of data. The code is running till an iteration where it crashes and returns a segmentation fault core dumped
(the memory used is always closed to 2g mem=1805804kb,vmem=1926132kb, seems like the code can't go beyond) .
But If i try to fit at this last iteration the code works perfectly, so i think that I'm not trying to access a variable at a wrong index but it might be dynamic memory allocation ( a pointer to a matrix that I didn't free or something like that). The code is complex and calls too many routines, I'm combining cimg , c++ and old c. How can I track this error?
View 19 Replies
View Related
Mar 24, 2013
So here is the C code:
Code:
#include <stdio.h>
int main(void)
{
//2D Array
int array[2][2];
int number = 1;
}
[code]....
The array is not filled incorrectly for some reason, more specifically the first row.The first two cycles of the for loop seem to work correctly. One if the bugs seems to occur on the third. when array[0][2] is filled with number 7, for some reason array[1][0] changes it value to 7 as well.
View 2 Replies
View Related
Mar 21, 2013
I'm writing a program with a class containing a private std::vector<bool>. I chose bool because the vector represents a 2D array (think grid) and I only need 2 states per cell. I kept it one-dimensional as this hardly complicates things.
My problem is that I don't know how to initialize the vector, i.e. fill it with 0's.
The grid's resolution is not known at compile time, so I imagine I have to set the size (and content) of the vector in the class constructor.
Here's what I have tried among several things:
Code: World::World(const u_short worldsize)
{
grid.reserve(worldsize * worldsize); // grid is the private vector; square dimensions.
std::fill(grid.begin(), grid.end(), 0);
std::cout << grid.size();
} The output is 0. Only std::vector::push_back seems to have an effect on size(), but judging by its description, it doesn't look like the right candidate to populate a vector with zeros. Correct me if I'm wrong.
Frankly I expected line 3 to set the vector's size.
View 5 Replies
View Related
Jun 25, 2013
I have this snippet :
Code:
#include<stdio.h>
#define LEN 3
int main(void)
{
int a[LEN] = {0} , b[LEN] = {0} , i = 0;
[Code] ....
/* OUTPUT :
a[0] = 2 , b[0] = 1
a[1] = 0 , b[1] = 3
a[2] = 0 , b[2] = 0 */
The problem is some elements of the array would be remained zero and unused. Is there any solution about this wasting of memory?
View 7 Replies
View Related
Mar 24, 2013
Assuming that we have :
Code:
int arr2d[rows][columns] ; // Not valid syntax of course ... let be arr2d rows * columns size
for(int i=0; i<rows; i++)
for(int j=0; j<columns; j++)
arr2d[rows][columns] = some_value;
What is the complexity? I believe O(n) and not O(n^2) on this case because if you have 3*3 size you would put 9 elements (from 9 elements input of course)... for each input you have one insertion and that is the meaning. Same as 4*4 size 16 input times 16 insertions .. or 5*5 and so forth...
View 8 Replies
View Related
Oct 6, 2013
What am I doing wrong here.
Code:
#include <stdio.h>
#define N 25
int main () {
int s,min,sec,total,mins;
float speed,splits[N];
[Code] ....
View 2 Replies
View Related
Feb 1, 2013
How to fill a vector with structs that are read in from a separate file. Each line in the file would read for example "Doe John M 26" for the name of the person, gender and age. I just need to get pointed in the right direction so I can get this started.
View 2 Replies
View Related
Jun 20, 2013
I am having a trivial trouble on how to create three different arrays from a text file. I am a beginner in C++. I have a .txt file containing a string of 'float' values as below:
0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
1.3
//----------------------
Now, I want to make three arrays, p1[], p2[], and p3[] from them, so that
p1[] has elements from line: 1, 4, 7, ...,
p2[] has elements from line: 2, 5, 8, .., and
p3[] has elements from line: 3, 6, 9,... of the .txt file.
My original file has a huge amount of data, so I would have to use a loop, but I cannot think of a way to fill my arrays as described.
View 3 Replies
View Related
Mar 2, 2013
I am trying to do binary filling. This is the Constructor code, you will see a couple of integer arrays:
Doc::Doc ( int useridValue , string firstnameValue , string lastnameValue , string deptValue
, int dayinhourValue[] , int dayinminValue[] ,int dayouthourValue[] , int dayoutminValue[]
, int timeperpatValue , int appValue[] , int applimValue[] ) {
setuserid(useridValue);
[Code] ....
it gives an error:
void Doc::setdayinhour ( intdayinHOUR[] ){
for(int i=0;i<7;i++) {
dayinhour[i]=dayinHOUR[i];//highlighted area
} }
I am using this code for filling:
void main () {
Doc r;
ofstream outDoc("docdata.dat" , ios::out | ios::binary);
if (!outDoc) {
cerr << "File Could Not Be Opened" << endl;
[Code] ....
View 5 Replies
View Related
Mar 21, 2015
I'm building a project in my free time and in the last part of it I need to fill a form of a website and then 'hit submit', but how it can be done. Btw the form is a 'POST' form so it's harder than a 'GET' one.
View 2 Replies
View Related
Jul 2, 2013
For some reason I am having problems doing this simple task. I just want to run a query in Access to select a field from the table that is "like" the string being inputted.
However, the dataset comes back empty...well not empty, I get a column name but no data. So when I type in
ds.Tables[0].Columns[0].ColumnName
I get the column name coming from the database ("ParcelNumber"). When I run the query inside Access I receive the desired information. But when I try to check for the info in code
ds.Rows[0]
I get "There is no row at position 0".
public DataTable FindFullPNusingPartialPN(string _parcelNumber) {
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=" + ConfigurationManager.AppSettings["TaxCert_Database"]);
DataTable dt = new DataTable();
[Code] ....
View 1 Replies
View Related
Jul 5, 2012
I have a question about allocating 2MB memory then filling it...
@ Platform:
=> DOS, and application is compiled/linked via Watcom C + DOS32/A (...memory model is "flat" mode)
@ phenomenon:
1. I allocate 2M memory by calloc() function. Then I got "!NULL" and it means allocating 2MB memory is ok( right ? )
2. then I tried to "fill" this 2MB memory by for loop(one byte by one byte) like below:
for( DWORD i=0; i<0x200000; i++) {
*((BYTE *)(A[0].B[0]->C) + i ) = 0x5A; // C is 4-byte address value
}
here :
* DWORD means "unsigned long(4-byte)" and 0x200000 means "2MByte"
* in actual case the value of pointer(to allocated memory) is 3019AF3C(~768MB) <- running in flat mode...
3. after filling this range of memory(2MB) the application crashed...
@ my observations:
1. if allocating 2MB and no fill(no write data to memory) => OK
2. if allocating 2MB and just fill the former 32 bytes => OK
3. if allocating 4KB and fill all => OK
my question is: why can't I filling this 2M memory totally "even memory allocation succeeds" ?
View 5 Replies
View Related
Oct 5, 2014
I am currently working on writing a word search program. However, I am stuck on reading the used input into the 2-D array. The code I've posted below is only dealing with the user input (I'll work on the word search part once I know i am correctly reading in the user input). I know the coding is bad practice with the use of hexadecimal, and getchar() ect. But I am currently using a microblaze microprocessor and this is just the way microblaze can interpret the information. As for the infinite while loops...that can be changed just trying to figure out how.
My question is how could I change my code to correctly read in the user input into the 2-D array?
Code:
#include "platform.h"#include "xparameters.h"
#include <stdlib.h>
#include <stdio.h>
#define MAX 20
int main() {
char grid[MAX][MAX], word[30];
int i, j, arr[2],num;
[Code] ....
View 10 Replies
View Related
Jan 12, 2013
Just trying to fill a dynamically allocated array with values then I want to print out the values using pointer method:
#include <iostream>
using namespace std;
long * extend_arr(long int arr[], long int length, long int val) {
long * array2 = new long [length + 1];
for (int J = 0; J < length; ++J)
array2[J] = arr[J];
[Code] ....
When this runs, I get an array with random numbers in it. For example, just trying to print the first value in *Block gives me random numbers each time. What is wrong with this as to why it is not holding the right values?
The extend_arr works perfectly fine, because when I try to access the values in the array using indexes (arr[0], arr[1], etc) it shows the right output, but using pointers does not. How can I make it work?
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 134561 23 29 640 112
As you can see, the primes end at 97 but it just keeps printing more
It now prints the correct values in the function if I set the last value in the array to 0. arr[length] = 0;
Now if I wanted to print the values in the array within main, why is that not working?
Nvm, I just changed the void function to return a pointer to an array.
View 2 Replies
View Related
Jul 27, 2013
So I'm trying to fill a vector with pointers to account objects.
std::vector<account*> fill_vector() {
std::vector<account*> temp;
std::ifstream s;
std::string str;
s.open("H://account2.dat");
[Code] ....
The accounts have four different types and I am supposed to skip over ones that have an invalid account type and throw an exception:
account* factory(std::string account_code, std::string first_name,
std::string last_name, char type, double balance) {
try {
if(type == 'A') {
simple_account *a = new simple_account(account_code, first_name, last_name, balance);
[Code] .....
My problem is they program will not skip over my rejected accounts.. still adds them to the vector but I cant figure out why!
View 8 Replies
View Related
Mar 31, 2013
Here is what I have going on.
Code:
struct client {
char firstName[stnd];
char lastName[stnd];
long ID;
char email[stnd];
float funds;
float wager;
}
typedef client...I would like to have these fields filled with this function below and stored in an array... Basically Multiple users and this is my function for it, if I can get it to work proper -.- .... I don't get syntax errors but I do get warnings
Code:
void getct(client *cl, int *pclientCounter) {
char input[buff];
char *pinput = NULL;
int typef = 0;
int lengthf = 0;
}
[code]....
View 3 Replies
View Related
Apr 20, 2014
I'm having a problem filling a vector from a file. Basically, it is adding an empty element at the end. I'm new to Qt and haven't worked with file streams much so how to stop the stream before it adds the extra element.
void gui::get_data() {
mileage.clear();
QFile file(file_label->text() + ".txt");
QTextStream in(& file);
float m;
float g;
QString d;
[Code] ....
But, if I add another element to the vector and write that the file look like this.
//file after adding element
132654 0 02132014
132654 0 02132014
0 0
132998 22 02202014
I have it set to append at the moment so that is why the first line is repeated. I figure the problem is with if(in.atEnd()). I could fix it by deleting the last element right after adding it, but that seems like more of a hack than anything else.
View 3 Replies
View Related
Dec 25, 2014
int firstarray[12][7] = { };
double firstClass(int airplane, int seats, double price)
{cout<<setw(60)<<"---------------------...
cout<<setw(60)<<"You are in **first class** booking screen
[Code] .....
The thing is, The first 2 rows having 7 columns like this:
0000000
0000000
User gives an input of seats reserved, for example user inputs 3 seats, then the program should give output like:
1110000
0000000
I will not ask any specific seat but put assign seats randomly where available.. How to do it?
View 5 Replies
View Related
Sep 2, 2014
I am trying to fill an array with blank spaces and instead i get the number 32 over and over, i think this is the ANSI code for that character. how do i get the character itself?
char values[max];
for(o=0;o<=max;o++)
{
values[o]=' ';
printf("%2d ", values[o]);
}
printf("
");
View 1 Replies
View Related
Jul 26, 2012
Project compile successfully but console turn off with "Windows " with error doesn't print or get anything
Code:
#ifndef Point_HPP // anti multiply including gates
#define Point_HPP
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
[Code] .....
View 2 Replies
View Related
May 17, 2013
I wrote a Simon game, and wanted to save the top 10 scores. I was working right, until I decided I wanted to still be able to read the file if someone enters a name containing spaces. Now, the results aren't right.
void FillScoreList(string Simon_Names[], int Simon_Scores[]) {
ifstream Simon_HiScores("Simon_Data.txt");
if (Simon_HiScores.is_open()) {
for( int x=0;x<10;x++){
[Code] ....
Even without trying to read names with spaces, I'm getting
dad 1
0
340176
0
... either a long number or a zero. No names
View 4 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 6, 2015
I am making a tictactoe program that requires me to have a 3 by 3 two dimensional array of integers, in which the constructor should initialize the empty board to all zeroes. The program complies, but it keeps outputting garbage values and i'm not entirely sure why, My print function isn't complete yet, since I want to print out the array in a tic tac toe format, but i'm more concerned with why it's printing garbage values, here is my code:
Code:
// header file
#include <iostream>
#include <array>
[Code] ....
View 7 Replies
View Related