C++ 如何创建不同大小字符串的动态二维数组
我想读一个.txt文件 .txt文件将包含N行和M列 txt文件中的每个单词都有不同的长度 示例txt文件:C++ 如何创建不同大小字符串的动态二维数组,c++,c,c++11,C++,C,C++11,我想读一个.txt文件 .txt文件将包含N行和M列 txt文件中的每个单词都有不同的长度 示例txt文件: Suppose N = 4 rows Suppose M = 5 cols txt文件的内容: Suppose N = 4 rows Suppose M = 5 cols 我必须做的事情: Suppose N = 4 rows Suppose M = 5 cols 我必须将这些字符串存储到一个2D字符串数组中,使其看起来像这样: arr[4][5] = [aa
Suppose N = 4 rows
Suppose M = 5 cols
txt文件的内容:
Suppose N = 4 rows
Suppose M = 5 cols
我必须做的事情:
Suppose N = 4 rows
Suppose M = 5 cols
我必须将这些字符串存储到一个2D字符串数组中,使其看起来像这样:
arr[4][5] =
[aa bbb cc dddddddd eeee]
[aa bbbbbbbbbbbb cc ddddddddddd eeee]
[aaaaaaaaaa bb cc d e ]
[a b c d eeee]
我知道如何创建integer的动态2D数组及其工作原理:
int** arr;
int* temp;
arr = (int**)malloc(row*sizeof(int*));
temp = (int*)malloc(row * col * sizeof(int));
for (int i = 0; i < row; i++)
{
arr[i] = temp + (i * col);
}
int count = 0;
//setting values in 2-D array
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
arr[i][j] = count++;
}
}
int**arr;
内部*温度;
arr=(int**)malloc(row*sizeof(int*);
temp=(int*)malloc(row*col*sizeof(int));
对于(int i=0;i|
但是,当我试图对弦做同样的事情时,它崩溃了
string** arr;
string* temp;
arr = (string**)malloc(row*sizeof(string*));
temp = (string*)malloc(row * col * sizeof(string));
for (int i = 0; i < row; i++)
{
arr[i] = temp + (i * col);
}
//setting values in 2-D array
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
arr[i][j].append("hello"); // CRASH here !!
}
}
字符串**arr;
字符串*temp;
arr=(字符串**)malloc(行*sizeof(字符串*);
temp=(字符串*)malloc(行*列*大小(字符串));
对于(int i=0;i|
如何将每个单词存储在数组中???
这是我写的:
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <vector>
#include <map>
#include <fstream>
#include <string>
#include <algorithm>
#include <assert.h> /* assert */
using namespace std;
vector<string> readFile(const string file, int& row, int& col)
{
vector<string> buffer;
ifstream read(file);
string line;
char * writable = NULL;
if (read.is_open())
{
int temp_counter = 0;
while (!read.eof())
{
std::getline(read, line);
writable = new char[line.size() + 1];
std::copy(line.begin(), line.end(), writable);
writable[line.size()] = '\0'; // don't forget the terminating 0
if (temp_counter == 0)//
{
row = std::stoi(line);
++temp_counter;
}
else if (temp_counter == 1)
{
col = std::stoi(line);
++temp_counter;
}
else
{
buffer.push_back(line);
}
}
}
// don't forget to free the string after finished using it
delete[] writable;
return buffer;
}
void create2DDynamicArray(std::vector<string>&v, int row, int col)
{
string** arr;
string* temp;
arr = (string**)malloc(row*sizeof(string*));
temp = (string*)malloc(row * col * sizeof(string));
for (int i = 0; i < row; i++)
{
arr[i] = temp + (i * col);
}
//setting values in 2-D array
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
arr[i][j].append("hello");
}
}
}
int main()
{
vector<string> myvector;
int row=0;
int col=0;
myvector = readFile("D:\\input.txt", row, col);
create2DDynamicArray(myvector, row, col);
getchar();
return 0;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括/*断言*/
使用名称空间std;
矢量读取文件(常量字符串文件、int和row、int和col)
{
向量缓冲区;
ifstream读取(文件);
弦线;
char*writable=NULL;
如果(读。是否打开())
{
int temp_计数器=0;
而(!read.eof())
{
std::getline(读取,行);
writable=新字符[line.size()+1];
std::copy(line.begin(),line.end(),可写);
可写的[line.size()]='\0';//不要忘记终止0
如果(临时计数器==0)//
{
行=标准::stoi(行);
++温度计数器;
}
否则如果(临时计数器==1)
{
col=std::stoi(直线);
++温度计数器;
}
其他的
{
缓冲器。推回(线路);
}
}
}
//使用完后别忘了松开绳子
删除[]可写;
返回缓冲区;
}
void create2DDynamicArray(标准::向量&v,整数行,整数列)
{
字符串**arr;
字符串*temp;
arr=(字符串**)malloc(行*sizeof(字符串*);
temp=(字符串*)malloc(行*列*大小(字符串));
对于(int i=0;i|
txt文件如下所示:
四,
五,
aa bbb cc DDDDDD eeee
aa BBBBBBBBBBCC DDDDDDDDD EEE
AAAAAAAA bb cc d e
c c d EEEE 在C++中不要使用<代码> MalcC< <代码>。它不会运行字符串的构造函数,因此不会为其中存储的动态字符数组分配空间。请尝试使用
new[]
运算符或智能指针
string **arr;
arr = new string*[height];
for (int i = 0; i < height; i++)
arr[i] = new string[width];
字符串**arr;
arr=新字符串*[高度];
对于(int i=0;i
C++ >代码> string <代码>只是动态<代码> char 数组的包装器,必须初始化(它应该分配给它的内存)。通过使用
malloc
您不调用构造函数,从而导致访问未分配的内存区域。如果您真的想使用2D数组并使用malloc
分配的内存,我的建议是从string**
转换为string***
类型,如:
iff流(您的文件名);
字符串***arr;
arr=(字符串***)malloc(行*sizeof(字符串**));
对于(int i=0;i>*arr[i][j];//或arr[i][j]>追加(“你好”);
}
}
<>但如果真的C++项目考虑使用<代码>向量< /代码>,甚至使用<代码>新< /代码>代替ifstreamf(“tmp.txt”);
字符串**arr;
arr=新字符串*[行];
对于(int i=0;i>arr[i][j];
}
}
//显示文件内容
对于(int i=0;i
cout我建议避免碎片,使用真正的2d数组
在C语言中,由于C99可以使用VLA(可变长度数组):
C中没有方法,这是编译不出来的,为什么要混合C和C++?选择其中一个。< /P> < P>不要使用<代码> MalOC >,避免<代码>新< /Calp>/<代码> NeX[]/Cord>,并使用RAII容器:
std::vector<std::vector<std::string>> readFile(const std::string& filename)
{
std::ifstream file(filename);
int row;
int col;
file >> row >> col;
std::vector<std::vector<std::string>> words(row, std::vector<std::string>(col));
for (auto& rows : words) {
for (auto& word : rows) {
file >> word;
}
}
return words;
}
std::vector readFile(const std::string和filename)
{
std::ifstream文件(文件名);
int行;
int col;
文件>>行>>列;
标准::向量字(行,标准::向量(列));
用于(自动行:字(&W){
用于(自动和文字:行){
文件>>word;
}
}
返回单词;
}
向量也是动态数组,但需要对用户隐藏的指针进行跟踪
如果你决定使用向量
arr[i][j].append("hello"); // CRASH here !!
std::vector<std::vector<std::string>> readFile(const std::string& filename)
{
std::ifstream file(filename);
int row;
int col;
file >> row >> col;
std::vector<std::vector<std::string>> words(row, std::vector<std::string>(col));
for (auto& rows : words) {
for (auto& word : rows) {
file >> word;
}
}
return words;
}
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main () {
cout<<"\nDynamic 2D Array.\n\n";
// create string vector
vector<string> vArray;
// create one line string
string line;
// open file for reading
ifstream fileToRead("d2d.txt");
while (getline(fileToRead, line)){
// fuse (store) line from file in vector
vArray.push_back(line);
}
fileToRead.close();
// display results
for (int i=0; i< vArray.size();i++){
cout<<" [ "<< vArray[i] <<" ] \n";
}
cout<<"\nPress ANY key to close.\n\n";
cin.ignore(); cin.get();
return 0;
}