Nov 28 2009

Remove items from deque, list or vector in a loop

Category: C++Kristou Mehrez @ 12:42 am

Recently I needed to delete items from a sequence like a deque , list or vector from a for loop, I figured out that I have to do something like this:

for(iter = list.begin(); iter != list.end(); ++iter)
{
// your code
iter = list.erase(iter);
--iter;
// your code
}

It looks basic but it took me a while to figure it out so I wanted to share in case somebody need it.

Tags: , , , , , ,


Jun 13 2009

PHP: Read file line-by-line

Category: PHPKristou Mehrez @ 7:46 am

[code="php"]
< ?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.

$lines = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : " . htmlspecialchars($line) . "
\n";
}
?>
[/code]

Tags: , , , , , ,