- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
QStringList list;
QStringList::iterator i, j;
//...
i = qLowerBound(list.begin(), list.end(), value);
j = qUpperBound(list.begin(), list.end(), value);
while (i != j) {
processItem(*i);
++i;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
Всего: 33
+1001
QStringList list;
QStringList::iterator i, j;
//...
i = qLowerBound(list.begin(), list.end(), value);
j = qUpperBound(list.begin(), list.end(), value);
while (i != j) {
processItem(*i);
++i;
}
Пахучее пятнышко из брошюры "Qt 4's Generic Algorithms"
+1009
#pragma once
#include <vector>
#include <algorithm>
#include <exception>
using namespace std;
template<typename nodeT>
class Tree
{
Tree* root;
Tree(Tree* _root, nodeT value)
: root(_root)
, Node(value)
{
}
vector<Tree> children;
public:
nodeT Node;
Tree(void) : root(nullptr) { }
Tree(const Tree& value)
: children(value.children)
, Node(value.Node)
, root(value.root)
{
}
virtual ~Tree(void) { }
const Tree& operator=(const Tree& value)
{
if(&value != this)
{
children = value.children;
for_each(children.begin(), children.end(), [this](Tree& tree)
{
tree.root = this;
});
Node = value.Node;
root = value.root;
}
return *this;
}
Tree& Root()
{
if(root == nullptr)
{
throw exception("already root");
}
return *root;
}
bool IsRoot() const
{
return root == nullptr;
}
Tree* Push(nodeT node)
{
children.push_back(Tree(this, node));
return &children.back();
}
Tree& operator[](typename vector<Tree>::size_type index)
{
return children[index];
}
vector<Tree*> Children()
{
vector<Tree*> result;
for(vector<Tree>::iterator i = children.begin(); i!=children.end(); i++)
{
result.push_back(&(*i));
}
return result;
}
typename vector<Tree>::iterator begin()
{
return children.begin();
}
typename vector<Tree>::iterator end()
{
return children.end();
}
};
Шаблон из http://govnokod.ru/6415.
+166
TagsTree ParseXML(const char file_name[])
{
ifstream input_file(file_name, std::ios::in);
string content;
if(!input_file.good())
{
throw "can't open xml";
}
while(!input_file.eof())
{
char buffer[256];
input_file.read(buffer, 256);
streamsize read_count = input_file.gcount();
content.append(buffer, buffer+read_count);
}
input_file.close();
auto Cleanup = [&content](const string& what_to_del) -> void
{
string::size_type pos = content.find(what_to_del);
while(pos != string::npos)
{
content.erase(pos, what_to_del.size());
pos = content.find(what_to_del, pos);
}
};
Cleanup("\n");
Cleanup("\t");
Cleanup(" ");
string::size_type comment_begin = 0;
string::size_type comment_end = 0;
for(;;)
{
string::size_type comment_begin = content.find("<!--", comment_end);
if(comment_begin == string::npos)
{
break;
}
string::size_type comment_end = content.find(">", comment_begin+3);
if(comment_end == string::npos)
{
throw "invalid xml: no comment closing brace";
}
content.erase(comment_begin, comment_end-comment_begin+1);
comment_end = comment_begin;
}
string::size_type header_begin = content.find("<?xml");
if(header_begin == string::npos)
{
throw "invalid xml: no header";
}
string::size_type header_end = content.find(">", header_begin+4);
if(header_end == string::npos)
{
throw "invalid xml: no header closing brace";
}
content.erase(comment_begin, header_end-header_begin+1);
auto CutTagAndContent = [](string& from, string& tag, string& content) -> void
{
string::size_type position = from.find('>');
if(position == string::npos)
{
throw "invalid xml: no tag closing brace";
}
tag = from.substr(1, position-1);
position = from.find("</"+tag+'>', position);
if(position == string::npos)
{
throw "invalid xml: no closing tag";
}
content = from.substr(tag.size()+2, position-tag.size()-2);
from.erase(0, position+tag.size()+3);
};
if(content[0] != '<')
{
throw "invalid xml: to root tag";
}
TagsTree result;
CutTagAndContent(content, result.Node.name, result.Node.content);
TagsTree::children_vectorT children;
children.push_back(&result);
do
{
for(auto i = children.begin(); i!= children.end(); i++)
{
while(!(**i).Node.content.empty())
{
if((**i).Node.content[0]!='<')
{
break;
}
TAG temporary;
CutTagAndContent((**i).Node.content, temporary.name, temporary.content);
(**i).Push(temporary);
}
}
children = EnlistChildren(children);
}
while(!children.empty());
return result;
}
Говнонедопарсер недоговноXML. Дерево тэгов - отдельная кучка.