传递字符串数组矢量C ++ [英] Passing array of strings to vector C++
本文介绍了传递字符串数组矢量C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我特林推字符串数组为矢量,像这样的:
的String [] URL = {www.facebook.com,www.orkut.com,www.yahoo.com};
INT32结果= URL.Length;
SetPolicyURL(URL,结果); SetPolicyURL(字符* URLpolicy [],__int32 noURL)
{
__int32 I = 0;而(I< noURL)
{
焦炭BUF [2512] = {0}; URLvector.push_back(URLpolicy [I]);
sprintf的(BUF,noURL =%d个URLpolicy [%d个=%s的URLvector [%D]。=%S,noURL,我,URLpolicy [我],我,URLvector.at(I).c_str());
MessageBoxA(NULL,BUF,0,MB_ICONERROR); ++我;
}
}
但是,当我尝试显示向量的元素,我只得到第一个数组元素的两倍,而在位置3,我没有得到这WHYY发生数组的第二个元素。
解决方案
URLvector.assign(URLpolicy,URLpolicy + noURL);
可用于字符串数组传递到一个载体
I am tring to push an array of strings to a vector, like this:
string []URL = {"www.facebook.com","www.orkut.com","www.yahoo.com"};
Int32 result = URL.Length;
SetPolicyURL( URL,result);
SetPolicyURL( char * URLpolicy[], __int32 noURL)
{
__int32 i = 0;
while ( i< noURL)
{
char buf[2512] = {0};
URLvector.push_back(URLpolicy[i]);
sprintf( buf," noURL = %d URLpolicy[%d] = %s URLvector[%d] = %s", noURL, i,URLpolicy[i] ,i, URLvector.at(i).c_str());
MessageBoxA(NULL, buf , 0, MB_ICONERROR);
++i;
}
}
But when I try to display the elements of the vector, I am only getting the first array element twice, and the 2nd element of the array in position 3. I am not getting whyy this is happening.
解决方案
URLvector.assign(URLpolicy,URLpolicy+noURL) ;
can be used to pass the array of strings to a vector.
这篇关于传递字符串数组矢量C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文