boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++里面如何最方便的表示这个数组的数组?
相关主题
几个C++的问题
static initialization dependency c++
一道 memset in C++的题
c++ 不自动initialize变量么?
抠字眼:assignment and initialize in C++
码工试题 (转载)
再问C++初始化问题。
static vector 怎么 initialize ?
关于C/C++里的Static variable的memory allocation/initializa
c++ initialize struct
相关话题的讨论汇总
话题: int话题: 数组话题: tuple话题: c++话题: vector
进入Programming版参与讨论
1 (共1页)
d*******n
发帖数: 524
1
比如有下面这个数组由3个小数组组成,每个小数组的size是不一样的?
{ {0, 2, 3},
{1, 3, 6, 6},
{2, 5}
}
请问怎么样最方便的表示这个数组的数组并且可以方便的初始化(就是说把这些数都写
到那个数据结构里面去)?
我想要的是类似Java里面的“数组的数组”的东西,java里面的数组是一个class,所以可以有
int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };
p***o
发帖数: 1252
2
想要方便就去用java把,不想要效率没必要折腾C++。
结构不变的图就是稀疏矩阵,一般都有文本输入格式,比如Matrix Market Exchange
Format,程序里面表示的方法也有很多,比如Compressed Row Storage,处理上亿
的点都没啥问题。

所以可以有

【在 d*******n 的大作中提到】
: 比如有下面这个数组由3个小数组组成,每个小数组的size是不一样的?
: { {0, 2, 3},
: {1, 3, 6, 6},
: {2, 5}
: }
: 请问怎么样最方便的表示这个数组的数组并且可以方便的初始化(就是说把这些数都写
: 到那个数据结构里面去)?
: 我想要的是类似Java里面的“数组的数组”的东西,java里面的数组是一个class,所以可以有
: int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };

d****p
发帖数: 685
3
boost tuple is just out there :-)

所以可以有

【在 d*******n 的大作中提到】
: 比如有下面这个数组由3个小数组组成,每个小数组的size是不一样的?
: { {0, 2, 3},
: {1, 3, 6, 6},
: {2, 5}
: }
: 请问怎么样最方便的表示这个数组的数组并且可以方便的初始化(就是说把这些数都写
: 到那个数据结构里面去)?
: 我想要的是类似Java里面的“数组的数组”的东西,java里面的数组是一个class,所以可以有
: int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };

d*******n
发帖数: 524
4
The thing is, most of the time, the developer cannot decide what language to
use......
You probably got my question wrong. I am not trying to discuss an high-level theoretical data-structure issue. I am only asking a handy trick to get some practical step done.
What I am really looking for is, in C++, how do you conveniently put such an
array of arrays in some datastructure, say, using a couple of rows of codes?
For example, if it's just an array of int, you can do the following as a
handy way t

【在 p***o 的大作中提到】
: 想要方便就去用java把,不想要效率没必要折腾C++。
: 结构不变的图就是稀疏矩阵,一般都有文本输入格式,比如Matrix Market Exchange
: Format,程序里面表示的方法也有很多,比如Compressed Row Storage,处理上亿
: 的点都没啥问题。
:
: 所以可以有

d*******n
发帖数: 524
5
For example, one thing I could do is the following, putting the integers in
a vector>.
d*******n
发帖数: 524
6
I just thought of a dirty trick that's only applicable to the case of single-
digit integers. That is, using char**. What I don't like about this solution
is that it uses 2-layer loops (a "for" and a "while"). Is there a way to do
this with only one loop?
#include
#include
using namespace std;
int main() {
char *s[]={"123","254","23","8640","1253","","253","80","2054","253"};
char** ss = s;
vector< vector > v(10);
for(int i = 0; i < 10; ++i){
char

【在 d*******n 的大作中提到】
: 比如有下面这个数组由3个小数组组成,每个小数组的size是不一样的?
: { {0, 2, 3},
: {1, 3, 6, 6},
: {2, 5}
: }
: 请问怎么样最方便的表示这个数组的数组并且可以方便的初始化(就是说把这些数都写
: 到那个数据结构里面去)?
: 我想要的是类似Java里面的“数组的数组”的东西,java里面的数组是一个class,所以可以有
: int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };

d****p
发帖数: 685
7
boost::tuple< boost::tuple, boost::tuple >
...x( boost::tuple<1, 2>, boost::tuple<3, 4, 5> >
...
x.get<0>().get<1>() = 2;
...
Whenever you resort to tricks, things are really going wrong. C++ has
everything you want for normal tasks.
p***o
发帖数: 1252
8
WoW!

【在 d****p 的大作中提到】
: boost::tuple< boost::tuple, boost::tuple >
: ...x( boost::tuple<1, 2>, boost::tuple<3, 4, 5> >
: ...
: x.get<0>().get<1>() = 2;
: ...
: Whenever you resort to tricks, things are really going wrong. C++ has
: everything you want for normal tasks.

p***o
发帖数: 1252
9
Why don't you put them into a file? Then you don't need a trick.
People have been working on these for tens of years. Google what
I said in the previous post and don't reinvent wheels.

single-
solution
do

【在 d*******n 的大作中提到】
: I just thought of a dirty trick that's only applicable to the case of single-
: digit integers. That is, using char**. What I don't like about this solution
: is that it uses 2-layer loops (a "for" and a "while"). Is there a way to do
: this with only one loop?
: #include
: #include
: using namespace std;
: int main() {
: char *s[]={"123","254","23","8640","1253","","253","80","2054","253"};
: char** ss = s;

p***o
发帖数: 1252
10
OK, let me tell you what to do.
int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };
int crs_value[] = {0,2,3,1,3,6,6,2,5};
int crs_index[] = {0,3,7,9};
now twoDimArray[i][j]=crs_value[crs_index[i]+j];

to
level theoretical data-structure issue. I am only asking a handy trick to
get some practical step done.
an
codes?

【在 d*******n 的大作中提到】
: The thing is, most of the time, the developer cannot decide what language to
: use......
: You probably got my question wrong. I am not trying to discuss an high-level theoretical data-structure issue. I am only asking a handy trick to get some practical step done.
: What I am really looking for is, in C++, how do you conveniently put such an
: array of arrays in some datastructure, say, using a couple of rows of codes?
: For example, if it's just an array of int, you can do the following as a
: handy way t

相关主题
c++ 不自动initialize变量么?
抠字眼:assignment and initialize in C++
码工试题 (转载)
再问C++初始化问题。
进入Programming版参与讨论
t****t
发帖数: 6806
11
not really. initializing complex class structure with constants are missing
from c++03. c++0x is extending it with std::initializer_list<>.

【在 d****p 的大作中提到】
: boost::tuple< boost::tuple, boost::tuple >
: ...x( boost::tuple<1, 2>, boost::tuple<3, 4, 5> >
: ...
: x.get<0>().get<1>() = 2;
: ...
: Whenever you resort to tricks, things are really going wrong. C++ has
: everything you want for normal tasks.

d*******n
发帖数: 524
12
Thanks a lot!
I did learn a lot (how to use tuple) starting from reading this post . But
this solution might not work as well as it looks.
The problem is, you need to hard-code your tuple structure, don't you?

【在 d****p 的大作中提到】
: boost::tuple< boost::tuple, boost::tuple >
: ...x( boost::tuple<1, 2>, boost::tuple<3, 4, 5> >
: ...
: x.get<0>().get<1>() = 2;
: ...
: Whenever you resort to tricks, things are really going wrong. C++ has
: everything you want for normal tasks.

c*******u
发帖数: 1269
13
struct A
{
A(int num1, int num2, int num3 ...)
{
para.line1[0]=num1;
...
};
struct
{
int line1[2];
int line2[3];
}para;

} ;
d****p
发帖数: 685
14
Initialize dynamic array in aggregate form? You will have it in c++0x.
std::vector myVec{1, 2, 3};
...

【在 d*******n 的大作中提到】
: Thanks a lot!
: I did learn a lot (how to use tuple) starting from reading this post . But
: this solution might not work as well as it looks.
: The problem is, you need to hard-code your tuple structure, don't you?

d*******n
发帖数: 524
15
Thanks!

【在 p***o 的大作中提到】
: OK, let me tell you what to do.
: int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };
: int crs_value[] = {0,2,3,1,3,6,6,2,5};
: int crs_index[] = {0,3,7,9};
: now twoDimArray[i][j]=crs_value[crs_index[i]+j];
:
: to
: level theoretical data-structure issue. I am only asking a handy trick to
: get some practical step done.
: an

1 (共1页)
进入Programming版参与讨论
相关主题
c++ initialize struct
Learn C++ 11 in 20 Minutes (视频)
pthread_create inside a constructor
请教大家一道C的面试题
C++: Static initialization dependency
问个copy constructor的问题
python 超级难题求救
关于数组动态分配的疑问???
谁给解释一下这个c question
one question about initializaiton list
相关话题的讨论汇总
话题: int话题: 数组话题: tuple话题: c++话题: vector