C ,基于价值输入从2D数组中选择
C++,Selecting from 2d array based on value-entry
我正在使用softflowd+nfdump
创建NetFlow数据,并将此数据存储在2D(String)Array
flows = new string *[flows_len];
for (int i=0;i<flows_len;i++)
{
flows[i] = new string[47];
}
我正在用C 写作。数组中的每个"行"代表一个流记录,47是NFDUMP显示的NetFlow数据的不同字段的数量。
我想根据IP创建一些统计信息(例如,每个IP有多少个连接流),但我不知道如何使用相同的IP获取这些行 - 流量(值SRCIP存储在流中[J] [4],我是新手C )。
预先感谢!
这是一个非常非常非常非常简单的示例
#include <vector>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iterator>
using namespace std;
typedef vector< string > StatInfo; // 47 enries
void print_stat_by_ip( const vector< StatInfo > & infos, const string & ip ) {
for ( int i = 0, count = infos.size(); i < count; i++ ) {
const StatInfo & info = infos[ i ];
if ( info[ 4 ] == ip ) {
copy( info.begin(), info.end(), ostream_iterator< string >( cout, ", " ) );
cout << endl;
}
}
}
int main()
{
vector< StatInfo > infos;
for ( int i = 0; i < 10; i++ ) {
StatInfo info;
for ( int j = 0; j < 47; j++ ) { // just filling them "0", "1", "2", ... , "46"
char c_str[ 42 ];
sprintf( c_str, "%d", j );
info.push_back( c_str );
}
char c_str[ 42 ];
sprintf( c_str, "%d", rand() % 10 );
info[ 4 ] = c_str; // this will be an IP-address
infos.push_back( info );
copy( info.begin(), info.end(), ostream_iterator< string >( cout, ", " ) );
cout << endl;
}
string ip_to_find = "5";
cout << "----------------------------------------" << endl;
cout << "stat for " << ip_to_find << endl;
cout << "----------------------------------------" << endl;
print_stat_by_ip( infos, ip_to_find );
}
您可以在这里找到它http://liveworkspace.org/code/3aaye8
老实说,我会考虑对您的容器进行重新考虑。以下内容使用标准的LIB数组,向量和多件事来完成我认为要寻找的目标。示例代码只会用字符串" A"," B"或" C"以及三个IP地址之一来填充表行。您应该向特殊说明的零件是根据IP地址索引表格的多件使用(尽管很容易对其进行改装以对任何任意列进行相同的操作)。
注意:有很多人比我更精确地熟练地熟练地熟练地熟练地使用性病算法,功能和容器用法。您可能的解决方案。
编辑 OP希望查看表中IP地址的计数,该代码已修改为main()
函数的尾部。还更新到不使用C 11功能。希望更接近OP可以使用的东西。
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <map>
#include <vector>
#include <string>
using namespace std;
// some simple decls for our info, table, and IP mapping.
typedef std::vector<std::string> FlowInfo;
typedef std::vector<FlowInfo> FlowTable;
// a multi-map will likely work for what you want.
typedef std::multimap<std::string, const FlowInfo* > MapIPToTableIndex;
// a map of IP string-to-unsigned int for counting occurrences.
typedef std::map<std::string, unsigned int> MapStringToCount;
int main(int argc, char *argv[])
{
// populate your flow table using whatever method you choose.
// I'm just going to push 10 rows of three ip addresses each.
FlowTable ft;
for (size_t i=0;i<10;++i)
{
FlowInfo fi(47); // note: always fixed at 47.
for (size_t j=0;j<fi.size();++j)
fi[j] = "A";
fi[0][0]+=i;
fi[4] = "192.168.1.1";
ft.push_back(fi);
for (size_t j=0;j<fi.size();++j)
fi[j] = "B";
fi[0][0]+=i;
fi[4] = "192.168.1.2";
ft.push_back(fi);
for (size_t j=0;j<fi.size();++j)
fi[j] = "C";
fi[0][0]+=i;
fi[4] = "192.168.1.3";
ft.push_back(fi);
}
// map by IP address into something usefull.
MapIPToTableIndex infomap;
for (FlowTable::const_iterator it = ft.begin(); it != ft.end(); ++it)
infomap.insert(MapIPToTableIndex::value_type((*it)[4], &*it));
// prove the map is setup properly. ask for all items in the map
// that honor the 192.168.1.2 address.
for (MapIPToTableIndex::const_iterator it = infomap.lower_bound("192.168.1.2");
it != infomap.upper_bound("192.168.1.2"); ++it)
{
std::copy(it->second->begin(), it->second->end(),
ostream_iterator<std::string>(cout, " "));
cout << endl;
}
// mine the IP occurance rate from the table:
MapStringToCount ip_counts;
for (FlowTable::const_iterator it= ft.begin(); it!=ft.end(); ++it)
++ip_counts[ (*it)[4] ];
// dump IPs by occurrence counts.
for (MapStringToCount::const_iterator it = ip_counts.begin();
it != ip_counts.end(); ++it)
{
cout << it->first << " : " << it->second << endl;
}
return 0;
}
输出
B B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
C B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
D B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
E B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
F B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
G B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
H B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
I B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
J B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
K B B B 192.168.1.2 B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
192.168.1.1 : 10
192.168.1.2 : 10
192.168.1.3 : 10
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 获取 2D 数组 c++ 中的所有数字对
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 如何创建一个所有行大小不同的 2D 数组,并且用户将指定每行将有多少个元素?
- 使用指针算法修改函数中的 2D 数组
- 在C++中初始化和打印 2D 数组
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- C++ 2D 数组写入多个位置
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 如何从initializer_list初始化 2D 数组
- 交换函数不是在 C++ 中交换 2D 数组的元素