C++ :: Deleting Iterator Inside A Double Loop

Aug 25, 2014

I know that if I erase a value inside an iterator, it becomes invalidated, and this is how you would normally fix it:

for(std::vector<int>::iterator it = list.begin(); it<list.end(); ) {
if((*it) == 5)
list.erase(it);
else
it++;
}

But what about a double loop? like this one:

This would delete all duplicates

for(std::vector<int>::iterator it = list.begin(); it<list.end(); it++) {
for(std::vector<int>::iterator it2 = list.begin(); it2<list.end(); it2++) {
if((*it) == (*it2))
list.erase(it2);
}
}

View 6 Replies


ADVERTISEMENT

C++ :: Deleting Value Stored Inside One Of Vectors

Feb 11, 2015

So I'm trying to delete a value stored inside one of my vectors but I can't accomplish this. My attempts are down below. I've commented out one attempt since it gives me errors. How can I do what I am trying to do?

#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int> >row;
vector<int> newColumn;

[Code] ....

View 10 Replies View Related

C Sharp :: Add ComboBox Inside ListView On Mouse Double Click Event?

Apr 9, 2013

I want to add ComboBox inside ListView on mouse double click event. On each double click, a new ComboBox should be added on the next row.

I tried it with the code below, but its not working.

private void form_DblClick(object sender, form_DblClickEvent e)
 {
ComboBox c;
this.Controls.Add(c = new ComboBox());
c.Items.Add("Input");
c.Items.Add("Delay");
c.Items.Add("Message");
c.Items.Add("comment");
listView1.Controls.Add(c);
 }

actually i want to add combobox will all of the above elements in one combobox..when i double click it again..in next line another combobox will be added with all elements...

View 1 Replies View Related

C/C++ :: Possible To Ask Yes / No Question Inside Of While Loop?

Apr 2, 2014

I'm trying to create a simple calc program that does all the elementary calculations. How to get it to add and multiply continuously without crashing. but now i need to figure out how to ask the user if they want to continue or not. I'm having trouble here because in the while statement i have a scanf which asks for the operator symbol. and then asks for number in the second scanf..

So basically a user would have to enter 'R' twice for the message to pop up! Also i'm not sure how i would quite the program if they put in a 'N' for no. would return 0; work? What if they press 'Y' for yes, what would the return have to be then?

Here's a snippet of the code
int main () {
double result;
double new_number;
char symbol;
result = 0;
while(1==scanf(" %c", &symbol) && symbol == 'R' && symbol =='r')

[Code] .....

View 9 Replies View Related

C++ :: File Handling Inside A While Loop?

Aug 5, 2013

I have two files like original.txt and replace.txt which has equal nbr of lines in both of them.

I need to loop through these two files line by line and replace the line in original.txt to the line in replace.txt

eg :

output.txt :

1|raj|65.4|Y|
1|ramesh|65.4|Y|

replace.txt :

1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|

What I need here is when reading of output.txt reaches second line of file "1|ramesh|65.4|Y|", it has to be replaced with the second line in replace.txt "Cannot be processed|1|ramesh|65.4|Y|".

1|ramesh|65.4|Y| --> Cannot be processed|1|ramesh|65.4|Y|

After the end of loop the contents of two files should be like :

original.txt :

1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|

replace.txt :

1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|

The files can have variable number of lines but both will have same number of lines each.

View 1 Replies View Related

C++ :: Make A Loop Inside A Condition?

Oct 26, 2013

I need to do make a loop inside a condition. Can it be done? I don't want to call another function to do it. Any way at all without calling separate function inside the if? I just want to do:

if (
for (int i = 0; i<=10; i++)
{
//stuff related to the for loop
} )
{
//stuff related to the initial if condition
};

View 7 Replies View Related

Visual C++ :: Why Char Is Inside Int Loop

Mar 8, 2013

Code:
......
static char str[BIG_STRING];
static char str_copy[BIG_STRING];
char **words;
int max_words = 10;

[Code] ....

I'm confused with this last [for] loop; How is ptr++ applied for non-integer value? Ptr is clearly a char, it comes from str, which reads string line from file.

I come from C# background, I have never met for...loop which irritates by using [somechar]++, not [someinteger]++;
What is actually going on there?

Some other similar example might be:

Code:
(iColor+(_parts[j]%length)*3),

where iColor is static unsigned char iColor[] array;

I would expect to see iColor[somevalue] + (_parts[j]%length)*3), but here in C++ I sometimes see that integer is being added directly to the array. What does it mean, what happens?

View 8 Replies View Related

C++ :: Mystic Logic Error In Double For Loop

Aug 16, 2014

In a record I have IDs of type int but some may repeat. These IDs also got sales of type float. I wish to compare the IDs number of times they appear and of match found accumulate their sales and put it in another struct. However if an ID is found which is new I pass it to a function to initiate sales..but my code never reach second ID, given I am trying with two IDs.. given IDs in transFile

ID Sale

12345 870
90909 100

I enter these two in transFile and match it with masterRecord. 12345 is matched and updated likewise but 90909 is never executed or matched in checkID function..

void sync_trans_files(unsigned short int count, master masterRecord[], char transFile[]){
const short int weeklyemployee=25;
//read number of records from transFile;
struct trans{

[Code].....

View 8 Replies View Related

C :: Random Number Generator Inside / Outside A Loop?

Nov 15, 2013

I've been working on a program on and off for around a week now and I've been struggling towards the end of the program.First of all, the program is a maths quiz which generates two random numbers per question.I'll give you one part of my code:

Code:

srand ( time(NULL) ); //seeds the random number generator
int score = 0;
int a = rand()%12 +1; //generates a random num between 1-12
int b = rand()%12 +1;
int c = a+b;
int d;
}

[code]....

I've basically copied the above code 10 times and changed the variables by going through the alphabet e.g.

Code:

int a = rand()%12 +1; //generates a random num between 1-12
int b = rand()%12 +1;
int c = a+b;
int d; all the way to

Code:

int an = rand()%12 +1;
int ao = rand()%12 +1;
int ap = rand()%12 +1;
int aq = an+ao-ap;
int ar;

Now what I'm going to do is remove all the declared variables and create a loop. But my problem is; If I wanted to declare four variables for e.g.

Code:

int a = rand()%12 +1;
int b = rand()%12 +1;
int c = rand()%12 +1;
int d = a+b-c;

Would I place the srand( time(NULL)); inside the loop? it's confusing because I know an example of a basic loop with an array would be:

Code:

#include <stdio.h>
#include <conio.h>
int main(void)
{

int test[5]={21,18,47,21,4};
int I;
int total=0;

for (I=0;i<5;i++)
total += test[I];
}

[code]....

how or where to include the random number generator in the loop and to make it ask 10 questions at random.

View 1 Replies View Related

C++ :: Difference Between Variable Declaration Inside And Outside For Loop

Sep 10, 2013

What is difference (memory allocation or any) between declaring a variable inside or outside the variable

program1:

#include<stdio.h>
int main() {
int i;
for (i=0;i=<100;i++) {

[Code] .....

Difference b/w program1 and program2. Its look both are same. but my compiler shows something difference.

View 1 Replies View Related

C++ :: Coding A Loop Inside A Class To Use In Main

Feb 22, 2013

I am writing a program where all work is done in the class methods. Main is used to call the methods. I need to know how to get a loop to work without any variables in main that can be used outside the methods. This is what I have in main:

#include <iostream>
#include <string>
using namespace std;
#include "FerryBoat.h"
int main() {
//create a constructor for a ferry boat
FerryBoat myBoat('B', 20, 'A');

[Code] ....

View 3 Replies View Related

C :: Switch Inside A While Loop - Write Min And Max To Get Negative Numbers

Nov 25, 2014

I got the while loop right... but how do i write the minimum and max to get negetive numbers like -88....

Code:
#include <stdio.h>#include <math.h>
#include <conio.h>
int gcd(int, int);
int main() {
int x, y, min, max, result, power, flag = 1;
char c;

[Code] ....

View 6 Replies View Related

C# :: Fibonacci Series - Adding Value In Array Inside While Loop

May 27, 2014

I am making a program to run Fibonacci series. I have created 2 array.

1)- 1st array holds only 0, 1
2)- 2nd array holds other values eg: 1, 2, 3, 5..........etc

I am using while loop to get the febonacci series and storing it in a 2nd array called int[] numbers.

After getting value using while loop, I am joing both the arrays using int[] final = arr.Concat(number).ToArray();

At last, I have used foreach loop to add the febonacci series into the listbox.

The problem I have is that, I cannot able to concat both the arrays. I tried to assign number array at the top of the while loop. so that number variable will be accessible outside the while loop. But I am getting a error.

See the code below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

[Code] .....

View 4 Replies View Related

C++ :: How To Get Iterator To The I'th Element

Jun 17, 2013

I'm making my first steps in STL, and I have a few question:

Is there a way to get an iterator to the i'th element in the collection (set or list), instead of just to the end or the begin?

And another question: Let's say I have an iterator, pointing to some element in my collection, and I use erase() (which takes as parameter an iterator that points to the soon-to-be erased element), what happens to that iterator? will it now point to NULL?

View 7 Replies View Related

C/C++ :: How To Declare Iterator

Aug 20, 2014

I have this class with template:

template<template<class, class> class ContainerType>
class Movie {
public:
typedef ContainerType<Actor*, std::allocator<Actor*> > Container;

and i have a member func to find actor in movie according to actor id:

const Actor* findActor(int id) const {
typename Container::iterator it;
Actor* p(NULL);
it=actors.begin();
std::for_each(it,actors.end(),(*it)->getId()==id?p=*it:0);
if (p==*actors.end()) return NULL;
return p;
}

I receive error that no match for operator= in it.

How to declare correct iterator ?the type of actors is Container..

View 6 Replies View Related

C++ :: Cannot Dereference Iterator At The End Of List?

Apr 8, 2014

Why this code works
Elenco e1;
e1.add(Persona("a","b"));
e1.add(Persona("c","d"));
e1.add(Persona("e","f"));
e1.add(Persona("e","f"));
e1.remove(2); //list of 4 elements

but this not work?
Elenco e1;
e1.add(Persona("a","b"));
e1.add(Persona("c","d"));
e1.add(Persona("e","f"));
e1.remove(2); //list of 3 elements

This is remove method:
Persona Elenco:: remove(int pos){
list<Persona> ::iterator iter=l.begin();
for(int i=0 ;i<pos;i++){
iter++;
}
return *(l.erase(iter)); //erase ritorna un iterator
}

View 4 Replies View Related

C++ :: Use Of Iterator Across Files And Classes

Nov 18, 2013

Here's a few parts of a program I'm working at. It does compile, and it does work as expected. Anyway Eclipse Kepler marks one line as a bug with the remark Field 'befehl' could not be resolved. The bug sign didn't show up when both classes were in one file.

ScriptInterpreter maintains and processes a vector of Objects, initialised with example data. An iterator of the vector keeps track of the current position while various methods process the data. I've copied the relevant lines only.

I can live with a few wrongly bug-marked lines in Eclipse. What I don't want is any hidden errors that express at some time later.

Is there anything wrong with the code? Anything that's not recommended and compiles anyway? Is anything c++11-specific about the questionable line?

AtomicCommand.h
class AtomicCommand {
public:
int befehl;

[Code] .....

Note that line 9 has a bug sign, too. Eclipse doesn't recognise all my c++11 code.

View 4 Replies View Related

C++ :: How To Control Iterator Of Multimap

Feb 22, 2013

I am running some simulation, in which i need to control the iterator of a multimap, like

multimap<double, int> mapp;
......

int n=6;
for(int i=0; i<5; i++) {
for(auto it = mapp.begin()+i*n; it!=mapp.begin()+(i+1)*n; it++) {
......

However this way of controlling is not possible for multimap. Why and how the make it work? Do I need to overload the operator +?

View 14 Replies View Related

C++ :: Can't Seem To Make STL Iterator Class

May 24, 2013

I can't seem to make the STL iterator class work how I need it to.I am implementing a multi list graph and I need to iterate through my STL list of Vertex pointer objects. I keep getting the error:

Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_List_iterator<_Mylist>' (or there is no acceptable conversion) and the same for != for my terminating condition.

template <typename V, typename E>
void Graph<V, E>::InsertEdge(V from, V to, E edge) {
list<Vertex<V>*>::iterator iter;
for(iter = m_Vertices.begin(); iter != m_Vertices.end(); ++iter)

[code].....

View 2 Replies View Related

C++ :: Difference Between Iterator And Pointer

Jan 21, 2013

I just figured out that some std functions (for example: copy) need the resource and target objects have iterators to work. Otherwise, the compiler rejects. In case of two arrays, declared as:

double myA[100] = {};
array<double, 100> myB = {};

myA[0] is like a pointer, myB.begin() an iterator, if I do not make any mistake. So, what is exactly the difference between the pointer and the iterator here? They all give the access to elements.

If I need the target of copy to be an array like myA which cannot give an iterator, is there a way to make the command "copy" work for it?

View 9 Replies View Related

C++ :: Get Iterator Position After Find If

Jan 25, 2013

I want to get the iterator position after to use find if:

std::list<Texture*>::iterator result = find_if(
texturelist.begin(),
texturelist.end(),
std::bind2nd<CompareTEX>(CompareTEX(),n_tex));
if (result != texturelist.end()) {
return // position result
}

View 5 Replies View Related

C++ :: Vector Iterator Not Incrementable

May 25, 2013

I am receiving the error: Vector Iterator not incrementable. However, when erasing I'm already re-setting 'it' and pre-incrementing at the end of the while-clause.what's wrong?

vector<st>::iterator it = map[0][0].begin();
while(it != map[0][0].end()) {
if((*it).val == 5) {

[Code] .....

View 1 Replies View Related

C/C++ :: Manipulating Data Through Own Iterator

Apr 10, 2014

We were given a task to use lists and iterators. We were supposed to make them from scratch. I'm done with that. The problems that I'm having are as following:

1. I'm not able to access the list made of Course datatype which is present in each Student instance. Does this mean I need to make an iterator for that course list inside the student class?

2. Similarly since I don't have direct access to The course list so I added the course into the Student list through the student objects not through the iterator. How can I do it through the iterator?

3. Printing of a particular student and his courses is not happening as my iterator made for student only prints out the students, not the courses present in their courselist. How to do that?

Here's the code

#include <iostream>
#include <string>
using namespace std;
const int ILLEGAL_SIZE = 1;
const int OUT_OF_MEMORY = 2;
const int NO_SPACE = 3;
const int ILLEGAL_INDEX = 4;

[Code] .....

View 1 Replies View Related

C/C++ :: Storing Map Iterator In Vector?

Jan 10, 2013

I'm trying out the code below; it runs and produces the output I'd expect, but could it lead to undefined behavior?

To test with, I show the key and wait for user input; if the input doesn't match the value, the iterator is stored in the vector.

map<string, string> test;
map<string, string>::iterator it;  
vector<map<string, string>::iterator> bad_input;  
string input;  
test["key 1"] = "value 1";

[Code] ....

I am also interested in knowing if I can use an iterator to walk through the vector? I tried

vector<map<string, string>::iterator>::iterator v_it;  
for(v_it = bad_input.begin(); v_it != bad_input.end(); ++v_it)
    cout << v_it-> ??  

However, I couldn't figure out how to access "first" and "second" using this method. Is it possible to do so?

View 2 Replies View Related

C++ :: Segmentation Fault On Multimap Iterator

Jan 5, 2015

I have created a multimap for my road points. The key refers to the road number and the values are vec3 points that make up the road.

I am trying to iterate through the values of each key point and create a road segment at each point on the road (except the last), adjust the values to be on the road points and then store them in a std::vector.

The RoadSegment constructor creates 6 vec3 points and pushes them onto a std::vector.

I have a segmentation fault in the line marked in bold
[for(mapIt = it.first; mapIt != it.second; ++mapIt)]

When i take out the lines creating the new objects and pushing them onto the std::vector it works fine.

std::vector<glm::vec3>::iterator SegIt;
for(int i = 0; i < m_genRoads->getKeyValueData().size(); i++)
{
int numberDesired = m_genRoads->getMultimapData().count(i) - 1;

[Code]...

View 1 Replies View Related

C++ :: Efficient Algorithm On List Iterator

Mar 6, 2013

I have a list and a vector, for example like

list<int> mylist = {1, 1, 1, 0, 0, 1, 0, 0}
vector<int> myvec;

I want to create an efficient algorithm to erase all element of value "1", and give these elements to a vector. My code (not efficient) is as follows

for(auto it = mylist.begin(); it != mylist.end(); it++) {
if(*it == 1) {
myvec.push_back(*it);
it = mylist.erase(it);
if(it != mylist.begin())
it--;
}
}

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved