首先介绍字符串分割函数:

char *strtok_s(
char *strToken, //字符串包含一个标记或一个以上的标记。
const char *strDelimit, //分隔符的设置
char **context //用于存储调用 strtok_s 之间位置信息
);

原来的函数strtok()因为具有线程不安全性,在linux内核中已被停止使用,需使用安全的strtok_s()。

The strtok_s function finds the next token instrToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call.

在第一次调用 strtok_s 函数跳过前导分隔符并返回指向在 strToken的第一个标记的指针,以空字符终止标记。 通过一系列 strtok_s 的调用,多个标记将被从 strToken 的其余部分拆开。 每个调用 strtok_s 通过插入 null 字符修改 strToken 在该返回的标记之后调用。 context 指针记录哪个字符串被读取,并记录将字符串下一个标记要读取的位置。 若要读取 strToken的下一个标记,请调用带有一个 strToken 参数的NULL 值的 strtok_s,并传递相同的 context 参数。 NULL  strToken 参数引起 strtok_s 搜索修改的 strToken的下一个标记。 strDelimit 参数可以接收一个调用到另一调用的任何值对,以便分隔符的设置可以更改。

因为 context 参数可用于 strtok 和 _strtok_l的静态缓冲区区域,同时在同一线程上分析两个字符串是可能的。(以上介绍来自MSDN库)

以msdn库的例子介绍:

// crt_strtok_s.c
// In this program, a loop uses strtok_s
// to print all the tokens (separated by commas
// or blanks) in two strings at the same time.
// #include <string.h>
#include <stdio.h> char string1[] =
"A string\tof ,,tokens\nand some more tokens";
char string2[] =
"Another string\n\tparsed at the same time.";
char seps[] = " ,\t\n";
char *token1 = NULL;
char *token2 = NULL;
char *next_token1 = NULL; //content
char *next_token2 = NULL; //content int main( void )
{
printf( "Tokens:\n" ); // Establish string and get the first token:
token1 = strtok_s( string1, seps, &next_token1);
token2 = strtok_s ( string2, seps, &next_token2); // While there are tokens in "string1" or "string2"
while ((token1 != NULL) || (token2 != NULL))
{
// Get next token:
if (token1 != NULL)
{
printf( " %s\n", token1 );
token1 = strtok_s( NULL, seps, &next_token1);
}
if (token2 != NULL)
{
printf(" %s\n", token2 );
token2 = strtok_s (NULL, seps, &next_token2);
}
}
}

Output:

Tokens:
A
Another
string
string
of
parsed
tokens
at
and
the
some
same
more
time.
tokens

此时可以处理从标准输入输入不定长的数组并保存的问题

#include <iostream>
#include<string>
#include<string.h>
#include<vector> using namespace std; void Split_input(vector<int> &v1) {
string s1;
getline(cin, s1);
cout << "s1 = " << s1 << endl; char* seps = " ";//空格,Note:可以是多个符号,只要待分割的字符串含有其中之一就可以分割
char *token = NULL;
char *next_token = NULL; char *buf = new char[strlen(s1.c_str()) + ];
const char *tmp = s1.c_str();
strcpy_s(buf, strlen(s1.c_str()) + ,tmp);//这两句是为了类型转换,因strtok_s 的实参必须是char*,而string类型转换为char* 类型结果为const char* 故指向const的指针不能被赋给指向非const的指针,所以应该用strcpy,也就是另开一块内存,把字符一个个复制过去
    token = strtok_s(buf, seps, &next_token);
while (token != NULL)
{
if (token != NULL)
{
v1.push_back(atoi(token)); // 将char转换为数字存入v1
token = strtok_s(NULL, seps, &next_token);
}
}
  delete[] buf;
vector<int>::size_type v1_count;
for (v1_count = ; v1_count < v1.size(); ++v1_count) //输出
{
cout << v1[v1_count] << " "<< endl;
}
} int main() {
vector<int> vec;
Split_input(vec);
system("pause");
}

上面用的是字符串切割,若是输入的一行是纯数组,就不用那么麻烦,其实这才是我最初想要的,ののの,就这样吧:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
int i;
vector<vector <int>> v;
vector<int> v1;
for (int j = ; j < ;++j)
{
v1.clear();//vector的clear()并不能删除内存,可用vector<int>(v1).swap(v1);原理是将大capacity置换为小的capacity
do
{
cin >> i;
v1.push_back(i);
} while (cin.get() != '\n');
v.push_back(v1);
} for (int j = ; j < ;++j) { //输出
int tmp;
for (tmp = ; tmp < v[j].size();++tmp)
{
cout << v[j][tmp] << endl;
}
cout << "\n" << endl;
}
}

从标准输入读取一行数组并保存(用的是字符串分割函数strtok_s() )的更多相关文章

  1. c++从文件中读取一行数据并保存在数组中

    从txt文本中读取数据存入数组中 #include <iostream> #include <fstream> #include <string> #include ...

  2. 【转】fscanf 跳过空格,读取一行

    fscanf(fp, "%s", sLineWord); 以上语句,在读取一行数据时,如何遇到该行数据有空格,那么读到空格处就停止,不再继续向下读. 若想遇到空格继续读取,读取完整 ...

  3. JAVA BufferedReader 类从标准输入读取数据

    1,从标准输入上建立输入流: BufferedReader localReader = new BufferedReader( new InputStreamReader(System.in)); S ...

  4. Opencv-Python:图像尺寸、图像的读取、显示、保存与复制

    Opencv-Python:图像尺寸.图像的读取.显示.保存与复制 原创 2017年11月23日 21:30:49 4440 在使用opencv的方法时,首先必须导入opencv包.新的opencv导 ...

  5. shell学习三十二天----read读取一行

    标准输入输出与标准错误输出 标准输入/输出可能是软件工具设计原则里最主要的观念了.他的构想是:程序应有一个数据来源,数据出口(数据要去哪里),以及报告问题的地方.他们分别叫做标准输入,标准输出和标准错 ...

  6. 编写javad代码实现使用Scanner从键盘读取一行输入,去掉其中重复字符, 打印出不同的那些字符

    package com.loaderman.test; import java.util.HashSet; import java.util.Scanner; public class Test2 { ...

  7. C# 文件操作 把文件读取到字节数组

    string zipfile = "c:\\a.zip"; //方法1 FileStream fs = new FileStream(zipfile, FileMode.Open) ...

  8. C#中创建、打开、读取、写入、保存Excel的一般性代码

    ---转载:http://hi.baidu.com/zhaocbo/item/e840bcf941932d15fe358228 1. Excel对象微软的Excel对象模型包括了128个不同的对象,从 ...

  9. /*使用PHP创建一个数组,保存5個员工的信息(ename/sex/salary/birthday/pic)*/

    <?php/*使用PHP创建一个数组,保存5個员工的信息(ename/sex/salary/birthday/pic)*/$empList=[    ['ename'=>'张学友','se ...

随机推荐

  1. Android 面试汇总<三>

    1.3 计算机网络 基础 Q:五层协议的体系结构分别是什么?每一层都有哪些协议? 技术点:网络模型.协议 思路:分条解释每层名字以及协议 参考回答: 物理层 数据链路层:逻辑链路控制LLC.媒体接入控 ...

  2. python threading.current_thread().name和.getName()有什么区别

    今天学到python多线程这块,想显示当前线程是主线程还是子线程.网上一搜,有个方法叫 threading.current().name 定海偶然 但是发现,同样的threading.current_ ...

  3. springboot2.0+mycat实验读写分离

    声明:用户到达一定程度,架构就必须要考虑,因为在这个前提下,读写分离,尤为重要. 1.搭建mysql主从复制 https://www.cnblogs.com/ywjfx/p/10264383.html ...

  4. Linux环境Nginx安装

    开始前,请确认gcc g++开发类库是否装好,默认已经安装. ububtu平台编译环境可以使用以下指令 apt-get install build-essential apt-get install ...

  5. ls | ethtool

    ls -lhS *.mp4|awk '{if($5>4000000) print $0}'ls -lhS *.mp4|awk '{if(($5>100000) && ($5 ...

  6. Vue+Python 电商实战

    安装webStorm  https://blog.csdn.net/qq_38845858/article/details/89850737 安装NodeJs  http://nodejs.cn/do ...

  7. Python的22个编程技巧,请收下!

    1. 原地交换两个数字 Python 提供了一个直观的在一行代码中赋值与交换(变量值)的方法,请参见下面的示例: x,y= 10,20 print(x,y) x,y= y,x print(x,y) # ...

  8. RAC FAILover详解(转载)

    Oracle  RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会 ...

  9. C#学习笔记二 (资源托管,泛型,数组和元组,运算符和类型强制转换)

     托管和非托管资源 1.托管资源是指GC管理的内存空间,非托管资源是指文件句柄,网络连接,数据库连接等. 2.方法中临时申请的变量,被存放在栈中.栈存储非对象成员的值数据.例如在方法中有B b=new ...

  10. Python学习之表的数据类型

    数据类型 数值类型 类型 大小 范围(有符号) 范围(无符号)unsigned约束 用途 TINYINT 1 字节 (-128,127) (0,255) 小整数值 SMALLINT 2 字节 (-32 ...