大家好我有以下问题我需要从文件构建一个矢量数组。 我设法通过将文件读入字符串缓冲区然后将其保存到以下向量中来创建向量。
vector<char> pattern(contents.begin(), contents.end());
但我有以下功能,我需要将矢量传递给。
void WuManber::Initialize( const vector<const char *> &patterns,
bool bCaseSensitive, bool bIncludeSpecialCharacters, bool bIncludeExtendedAscii )
如何从文件中读取可通过的向量到此函数。 谢谢你的帮助。
详细说明我需要它来执行以下操作
for ( size_t q = m; q >= B; --q )
{ // start loop -8-
unsigned int hash;
hash = m_lu[patterns[j][q - 2 - 1]].offset; // bring in offsets of X in pattern j
hash <<= m_nBitsInShift;
hash += m_lu[patterns[j][q - 1 - 1]].offset;
hash <<= m_nBitsInShift;
hash += m_lu[patterns[j][q - 1]].offset;
size_t shiftlen = m - q;
m_ShiftTable[ hash ] = min( m_ShiftTable[ hash ], shiftlen );
if ( 0 == shiftlen )
{ // start if -8-
m_PatternMapElement.ix = j;
m_PatternMapElement.PrefixHash = m_lu[patterns[j][0]].offset;
m_PatternMapElement.PrefixHash <<= m_nBitsInShift;
m_PatternMapElement.PrefixHash += m_lu[patterns[j][1]].offset;
m_vPatternMap[ hash ].push_back( m_PatternMapElement );
} // end if -8-
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cout << "usage: " << argv[0] << " <filename>\n";
return 2;
}
ifstream fin(argv[1]);
if (fin) {
stringstream ss;
// this copies the entire contents of the file into the string stream
ss << fin.rdbuf();
// get the string out of the string stream
string contents = ss.str();
cout << contents;
// construct the vector from the string.
vector<char> pattern(contents.begin(), contents.end());
Initialize( &pattern);
cout << pattern.size();
}
else {
cout << "Couldn't open " << argv[1] << "\n";
return 1;
}
return 0;
}
答案 0 :(得分:2)
该函数正在查找vector
个C字符串(这是const char *
容器中patterns
的最可能含义。
以下是如何构建一个:
std::vector<const char*> patterns;
patterns.push_back("my-first-pattern");
patterns.push_back("my-second-pattern");
如果您或您的团队设计了此功能,您可能需要建议将容器类型更改为更多C ++ - ish vector<string>
。
如果您正在从文件中读取模式,则可以按如下方式填充数组:
编辑(回复评论)
std::ifstream ifs("file_with_patterns.txt");
std::vector<const char*> patterns;
while (!ifs.eof()) {
std::string buf;
std::getline(ifs, buf);
patterns.push_back(strdup(buf.c_str()));
}
答案 1 :(得分:1)
如果contents
是std::string
,您可以在向量中存储指向string
数据的指针,并将其传递给函数:
vector<const char*> patterns;
patterns.push_back(contents.c_str());
// then pass patterns to Initialise
Initialise(patterns, bool, bool, bool);
// make sure you do not try to use `patterns` after you have changed `contents`
答案 2 :(得分:0)
我不确定,但也许不是虚空,让它成为一个向量并返回修改后的向量?
vector<char> WuManber::Initialize(const vector<const char *> &patterns, bool bCaseSensitive, bool bIncludeSpecialCharacters, bool bIncludeExtendedAscii )
{
//vector manupilations
return patterns;
}