從標准輸入讀取一行數組並保存(用的是字符串分割函數strtok_s() )


首先介紹字符串分割函數:

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()) + 1]; 
    const char *tmp = s1.c_str();
    strcpy_s(buf, strlen(s1.c_str()) + 1,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 = 0; 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 = 1; j < 4;++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 = 0; j < 4;++j) { //輸出 int tmp; for (tmp = 0; tmp < v[j].size();++tmp) { cout << v[j][tmp] << endl; } cout << "\n" << endl; } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM