#ifndef SPLIT_H
#define SPLIT_H
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <vector>
using std::string; using std::vector;
class split{
public:
// 文字列を文字列delで分割しリスト化する関数
vector<string> splits(string str, char del);
// 分割リストのstart番目からend番目までの要素を結合する関数
string splitjoin(string str, char del, int start, int end);
// 作業ディレクトリを変更する関数
int chCWD(string str, char del, int start, int end);
};
#endif
#include "split.h"
using std::string; using std::vector;
// 文字列を文字列delで分割しリスト化する関数
vector<string> split::splits(string str, char del) {
int first = 0;
int last = str.find_first_of(del);
vector<string> result;
while (first < str.size()) {
string subStr(str, first, last - first);
result.push_back(subStr);
first = last + 1;
last = str.find_first_of(del, first);
if (last == string::npos) {
last = str.size();
}
}
return result;
}
// 分割リストのstart番目からend番目までの要素を結合する関数
string split::splitjoin(string str, char del, int start, int end){
string result2;
vector<string> list = split::splits(str, del);
vector<string> list2;
for (int i = 0; i < list.size(); i++){
if (end < 0){
end += list.size();
}
if (i >= start && i <= end) {
list2.push_back(list[i]);
}
}
for (int i = 0; i< list2.size(); i++){
result2.append(list[i] + del);
}
return result2;
}
// 作業ディレクトリを変更する関数
int split::chCWD(string str, char del, int start, int end){
string new_str = split::splitjoin(str, del, start, end);
int rtn = chdir(new_str.c_str());
return rtn;
}
#include <Cocoa/Cocoa.h>
#include <iostream>
using std::cout; using std::endl;
int num = 1;
// Objective-C++
NSLog(@"Objective-C++ num %d",num);
// C++
cout << "C++ num " << num << endl;
--------------------------------------------------
出力
--------------------------------------------------
Objective-C++ num 1
C++ num 1